1

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');
            }
        });

1 Answer 1

1

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.