13

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!

prototype
  • 3,303
  • 2
  • 27
  • 42

2 Answers2

32

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
    });
prototype
  • 3,303
  • 2
  • 27
  • 42
0

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
})
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ragav
  • 29
  • 3