1

I am running node.js with mongoDB with mongojs as driver. I have the following issue,

I try to query business collection with _id array, if I try to build the query dynamically as below, I get array with length 0 back which is wrong.

 myFavoriteBusinessArray.forEach(function (iValue, j) {
            var temp = 'mongojs.ObjectId("' + iValue + '")';
            objectIdArray.push(temp);
        });
        var queryObject = {
            _id: {
                $in: objectIdArray
            }
        };
 db1.db.business.find(queryObject,
            function (err, business) {
                if (err) res.json(err);
                res.json(business);
            });
    }
    else {
        res.json({"login": "failed"});
    }

Where as If I try the following, then I am getting right array of 3 back.

db1.db.business.find({_id: { $in: [ mongojs.ObjectId("534fabb10648cd1c1b000002"), mongojs.ObjectId("52d664c15186ad103c000001"), mongojs.ObjectId("534ee1b4b51682bc30000002") ] }},
            function (err, business) {
                if (err) res.json(err);
                res.json(business);
            });

Could anyone please point me where I am going wrong? Many thanks in advance.

Regards, Chidan

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chidu Murthy
  • 688
  • 3
  • 10
  • 26

1 Answers1

2

You should replace:

var temp = 'mongojs.ObjectId("' + iValue + '")';

by:

var temp = mongojs.ObjectId(iValue);
Julio
  • 2,493
  • 4
  • 33
  • 53