13

我对 node.js/sails.js 有点陌生,并且想知道(如果可能的话)如何通过搜索它们的 id 来检索多个数据库条目 - MongoDB 文档中提到了类似的内容:

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

这是我尝试过的:

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

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

任何帮助,将不胜感激!谢谢!

4

2 回答 2

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
    });
于 2014-01-09T20:20:28.577 回答
0

这可以通过使用本机函数在风帆内使用 MongoDB 查询来完成。这个原生函数允许sails 运行数据库查询。

User.native(function(err, response) {
  response.find({ qty: { $in: [ 5, 15 ] } })
}).toArray(function (err, results) {
     //return the result
})
于 2017-09-26T07:51:49.277 回答