Question

I'm bit new at node.js/sails.js and was wondering (if possible) how to retrieve multiple database entries by searching for their ids - there is something like this mentioned in the MongoDB documentation:

db.inventory.find( { qty: { $in: [ 5, 15 ] } } )

And here is what i've tried:

// users param example: 12341243124, 1231231231, 21312313212
var users = req.param('users').split(',');

User.find({id: { $in: users }}, function (err, response) {
  // do something here
});

Any help would be appreciated! Thanks!

Was it helpful?

Solution

Sorry for bothering - as it turns out Waterline supports array parameters - so by changing the code above a bit i got this to work:

User.find()
    .where({id: users})
    .exec(function (err, response) {
        // do stuff
    });

OTHER TIPS

This can be done by using the MongoDB query inside the sails using the native function. This native function allows the sails to run the database queries.

User.native(function(err, response) {
  response.find({ qty: { $in: [ 5, 15 ] } })
}).toArray(function (err, results) {
     //return the result
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top