Newbie
Newbie

Reputation: 535

mongoosejs query fails when filtering using a string of keys as selector

I was attempting to query docs from a mongodb and have the system return only a few fields from the documents matching the query. I first tried the syntax listed below for the first query and it failed to return i.e. the callback was never called.

I then experimented some more with alternative syntax and was able get results from the second query listed below. I'd like to understand why the first query didn't work - have I misinterpreted the syntax?

this is mongoose 3.6.8 and mongo 2.4

TIA

First query

query.find({
    category:{
        $in: categoryArray
        }
        ,expiration:{$gte: new Date()}
        ,place:queryVal.destination
        }
        ,'_id expiration url' 
        ,function (err, docs) {
            if (err) {
                console.log(err);
            } else {
                console.log('queryJoin returned ' + docs.length + 'entries');
            }
        }
);

Second query

query.find({
        category:{$in: categoryArray}
        ,expiration:{$gte: new Date()}
        ,place:queryVal.destination
        })
        .select({_id:1, expiration:1, url:1})
        .exec(function(err, docs) {
            console.log('queryJoin returns');
            if (err) {
                console.log(err);
            } else {
                console.log('queryJoin returned ' + docs.length + 'entries');
            }
        });

Upvotes: 1

Views: 229

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312085

Your first attempt used Model.find syntax but you were trying to use it with Query#find which doesn't support a fields parameter. As such, Mongoose interpreted your field selection string as a callback which is why your actual callback didn't get called.

Upvotes: 1

Related Questions