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

1 Answer 1

2

You should replace:

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

by:

var temp = mongojs.ObjectId(iValue);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.