Mahahari
Mahahari

Reputation: 1011

Mongodb Or operator in Find Query

I have a query as below.

    Post.native(function(err, collection) {

        collection.find({
            $or: [ { id :id }, { parentid : id} ]
        },  function(err, result) {
            if (err)
                console.log({error: err});
            console.log(result);
        });
    });

But this returns zero results , even though i have one result satisfying {id : id} and two results satisfying {parentid : id}. I need three results to be printed.

Please correct me if my query is wrong. Any help would be appreciated.

Please help. Thanks a lot

Upvotes: 1

Views: 116

Answers (1)

Jason Joseph Nathan
Jason Joseph Nathan

Reputation: 7601

Here are some of the most common things I would do to debug this problem.

If you use the native driver and the fields you are querying are MongoIds, you need to ensure that ids are instances of ObjectId, i.e.

id = new mongo.ObjectID(id)

Also, mongodb natively stores its id as _id. Did you specifically create an id field?

Upvotes: 1

Related Questions