Mars Zhu
Mars Zhu

Reputation: 296

Mongo $in query works in mongo but not in sails

Basically the query works in mongo but not in sails controller:

    db.membermodel.find({identifier:{$in:["2","3","4"]}); // works

    MemberModel.find({
        identifier:{$in:["2","3","4"]},
    }).then(function(members){
        // doesn't work
    });

data returned:

    { "_id" : ObjectId("52d1a484f2b5e88cb5d4072c"), "identifier" : "2", "deviceToken" : "token2"}
    { "_id" : ObjectId("52d1a487f2b5e88cb5d4072d"), "identifier" : "3", "deviceToken" : "token3"}

Thanks, Mars

Upvotes: 0

Views: 985

Answers (2)

sgress454
sgress454

Reputation: 24948

This isn't the way to do in queries with Waterline. You simply set the attribute you're selecting to the array value:

MemberModel.find({
    identifier:["2","3","4"]
}).exec(function(err, members){
    ...
});

If you really need to use low-level Mongo features, you can get an instance of the native collection with

MemberModel.native(function(err, collection) {
   //do native mongo driver stuff with collection
}

Upvotes: 1

Max Rios
Max Rios

Reputation: 2256

Hard to understand how the model is being queried but I suggest you to "spy" what's Mongo getting as the MVC framework query, due to this is not a direct query to Mongo, it's passed through the framework. I'm quite sure you're still developing so you have access to your mongo instance, restart it using full profile (the trick is everything is slow under 1ms)

mongod --profile=1 --slowms=1 &

Tail the resulting log which normally is in

/var/log/mongodb/mongodb.log

with the command

tail -f /var/log/mongodb/mongodb.log

Send your query again and check what MongoDb is executing.

Upvotes: 0

Related Questions