Question

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

Was it helpful?

Solution

You should replace:

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

by:

var temp = mongojs.ObjectId(iValue);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top