1.6 KiB
1.6 KiB
title | localeTitle |
---|---|
Chain Search Query Helpers to Narrow Search Results | 链搜索查询帮助缩小搜索结果 |
链搜索查询帮助缩小搜索结果
- 创建但不执行查找查询
Model.find( {name: 'Leah'} )
- 将查询查询存储到变量中以供以后使用:
var findQuery = YourModel.find( {name: 'Leah'} )
- 要对数组进行排序:
yourArray.sort( {age: 1} ) // Here: 1 for ascending order and -1 for descending order.
- 要限制数组的大小:
yourArray.limit(5) // return array which has 5 items in it.
- 要从结果中隐藏某些属性:
yourArray.select( {name: 0, age: 1} ) // Here: 0 means false and thus hide name property; 1 means true so age property will show.
- 要执行此查询,您可以:
1)回调:
YourQuery.exec(function(err, docs) {
//do something here
})
或者2)承诺
YourQuery.exec.then(function(err, docs) {
//do something here
})
- 将它们连在一起:
Person.find({age: 55}).sort({name: -1}).limit(5).select( {favoriteFoods: 0} ).exec(function(error, people) {
//do something here
})
这是一个存根。 帮助我们的社区扩展它 。