Model.find(filter[, projection][, options][, callback])
html
查询条件使用 JSON 文档的格式,JSON 文档的语法跟 MongoDB shell
中一致。前端
{ field1: value1, field2: { operator: value2 } ... }
mongodb
Model.find() Model.find({})
Model.find({author:'dora'})
对比相关操做符shell
符号 | 描述 |
---|---|
$eq | 与指定的值相等 |
$ne | 与指定的值不相等 |
$gt | 大于指定的值 |
$gte | 大于等于指定的值 |
$lt | 小于指定的值 |
$lte | 小于等于指定的值 |
$in | 与查询数组中指定的值中的任何一个匹配 |
$nin | 与查询数组中指定的值中的任何一个都不匹配 |
Model.find({ age: { $in: [16, 18]} })
返回 age
字段等于 16
或者 18
的全部 document。express
逻辑相关操做符api
符号 | 描述 |
---|---|
$and | 知足数组中指定的全部条件 |
$nor | 不知足数组中指定的全部条件 |
$or | 知足数组中指定的条件的其中一个 |
$not | 反转查询,返回不知足指定条件的文档 |
// 相关语法 {$and:[ {expression1},{expression2}, ... ,{expressionN} ]} {$nor:[ {expression1},{expression2}, ... ,{expressionN} ]} {$or:[ {expression1},{expression2}, ... ,{expressionN} ]} {field: { $not: { <operator-expression> }}}
逻辑操做符中的比较包括字段不存在的状况。数组
Model.find( { age: { $not: { $lte: 16 }}}) // 返回 age 字段大于 16 或者 age 字段 不存在 的文档
字段相关操做符promise
符号 | 描述 |
---|---|
$exists | 匹配存在指定字段的文档 { field: { $exists: <boolean> } } |
$type | 返回字段属于指定类型的文档 {field: { $type: <BSON type> }} |
数据以下async
{ name: { first: "dora", last: "wang" } }
精确匹配,顺序、字段都必须一致。mongoose
Model.find({ name: { last: "wang", first: "dora" } }) // [] 找不到数据
使用点语法,可匹配嵌套的字段,其中字段名必须用引号引发来。
Model.find({ 'name.last': 'wang' })
符号 | 描述 |
---|---|
$all | 匹配包含查询数组中指定的全部条件的数组字段 |
$elemMatch | 匹配数组字段中的某个值知足 $elemMatch 中指定的全部条件 |
$size | 匹配数组字段的 length 与指定的大小同样的 document |
数据以下
{ year: [ 2018, 2019 ] } { year: [ 2017, 2019, 2020 ] } { year: [ 2016, 2020 ] }
查找数组中的至少一个值
可以使用精确查找写法 {field: value}
Model.find({ year: 2019 }); // { "_id" : ..., "year" : [ 2018, 2019 ] } // { "_id" : ..., "year" : [ 2017, 2019, 2020 ] }
查找数组中的多个值
使用 $all
查找同时存在 2019
和 2020
的 document
Model.find({ year: { $all: [ 2019, 2020 ] } }); // { "_id" : ..., "year" : [ 2017, 2019, 2020 ] }
操做符组合查询
可以使用操做符进行筛选,{<field>:{operator: value}}
,好比 $in
Model.find({ year: { $in: [ 2018, 2020 ] } }); // { "_id" : ..., "year" : [ 2018, 2019 ] } // { "_id" : ..., "year" : [ 2017, 2019, 2020 ] }
使用操做符组合查询 {<field>:{operator1: value1, operator2: value2}}
Model.find({ year: { $gt: 2019, $lt: 2018 } }); // { "_id" : ..., "year" : [ 2017, 2019, 2020 ] } // { "_id" : ..., "year" : [ 2016, 2020 ] }
数组字段包含知足查询条件的元素,能够是不一样元素分别知足条件也能够是同一个元素知足全部条件,如上例,是一个值知足大于2019的条件,另外一个值知足小于2018的条件。
$elemMatch 单个字段值知足全部查询条件
$elemMatch
查找数组字段中的值同时知足全部条件的 document
。
{field: { $elemMatch: { <query1>, <query2>, ... }}}
Model.find({ year: { $elemMatch: { $gt: 2016, $lt: 2018 } } }) // { "_id" : ..., "year" : [ 2017, 2019, 2020 ] }
数组下标查询
Model.find({ 'year.1': { $gt: 2019 } }) // { "_id" : ..., "year" : [ 2016, 2020 ] }
数组 year
的第二个值大于2019
。
数据以下
{author: [{name: "dora", age: 18 },{name: "wang", age: 16 }]}
精确查询
精确匹配,顺序、字段都必须一致。
Model.find({ author: { name: "dora", age: 18 } })
点语法查询
Model.find({ 'author.age': { $gte: 18 } })
$elemMatch 同时知足全部查询条件
Model.find({ "author": {$elemMatch: {name: 'dora', age:{ $lt: 18 }}}) // []
指定要包含或排除哪些 document
字段(也称为查询“投影”),必须同时指定包含或同时指定排除,不能混合指定,_id
除外。
在 mongoose 中有两种指定方式,字符串指定和对象形式指定。
字符串指定时在排除的字段前加 -
号,只写字段名的是包含。
Model.find({},'age'); Model.find({},'-name');
对象形式指定时,1
是包含,0
是排除。
Model.find({}, { age: 1 }); Model.find({}, { name: 0 });
使用 select() 方法定义
Model.find().select('name age'); Model.find().select({ name: 0 });
// 三种方式实现 Model.find(filter,null,options) Model.find(filter).setOptions(options) Model.find(filter).<option>(xxx)
options
选项见官方文档 Query.prototype.setOptions()
。
sort
:按照排序规则根据所给的字段进行排序,值能够是 asc
, desc
, ascending
, descending
, 1
, 和 -1
。limit
:指定返回结果的最大数量skip
:指定要跳过的文档数量lean
:返回普通的 js 对象,而不是 Mongoose Documents
。建议不须要 mongoose 特殊处理就返给前端的数据都最好使用该方法转成普通 js 对象。// sort 两种方式指定排序 Model.find().sort('age -name'); // 字符串有 - 表明 descending 降序 Model.find().sort({age:'asc', name:-1});
sort
和 limit
同时使用时,调用的顺序并不重要,返回的数据都是先排序后限制数量。
// 效果同样 Model.find().limit(2).sort('age'); Model.find().sort('age').limit(2);
Mongoose 中全部传入 callback
的查询,其格式都是 callback(error, result)
这种形式。若是出错,则 error
是出错信息,result
是 null
;若是查询成功,则 error
是 null
, result
是查询结果,查询结果的结构形式是根据查询方法的不一样而有不一样形式的。
find()
方法的查询结果是数组,即便没查询到内容,也会返回 []
空数组。
不传入 callback
时,查询方法返回的是一个 Query
实例,实例继承了 Query
原型 上的全部方法,所以返回的实例能够链式调用其它方法,从而组成查询链。
let query = Model.find({ name: 'Dora' }); query.select('name age -_id');
查询方法不传入回调函数时,获取查询数据的方式有两种:
1. exec()
使用 query
实例的 exec()
方法执行查询,即在链式语法的最后统一经过传入 callback
获取查询数据。
// 效果同样 Model.find( { name: /Dora/, age: { $gte: 16, $lt: 18 } }, 'name age -_id', { limit: 2, sort: 'age' }, (err,res)=>{ if (err) return handleError(err); console.log(res); } }); let query = Model. find({ name: /Dora/ }). where('age').gte(16).lt(18). select('name age -_id'). limit(2).sort('age'); query.exec((err, res)=> { if (err) return handleError(err); console.log(res); });
2. then()
使用 query
实例的 then()
方法将查询链看成 promise
来处理。
query.then( (res) => {console.log(res)}, (err) => {console.log(err)} );
使用 async / await
获取查询结果。
let res = await query; console.log(res);
Model.findOne(conditions[, projection][, options][, callback])
若是查询条件是 _id
,建议使用 findById()
。
并非全部 options
均可以用,所以链式调用 Query
原型上的方法时,也只能调用可用的方法。参考 Query.prototype.setOptions()
。
{}
对象形式。conditions
为 {}
、 null
或 undefined
,将任意返回一条数据。result
返回 null
。Model.findById(id[, projection][, options][, callback])
Model.findById(id)
至关于 Model.findOne({ _id: id })
。
?? 摘自官方
不一样之处在于处理id
为undefined
时的状况。findOne({ _id: undefined })
至关于findOne({})
,返回任意一条数据。而findById(undefined)
至关于findOne({ _id: null })
,返回null
。
测试结果为 findOne({ _id: undefined })
依旧返回 null
。因此也多是 mongoose 版本的问题,可是即便数据返回正常,依然建议 _id
查询使用 findById()
。
{}
对象形式。id
为 undefined
或 null
,result
返回 null
。result
返回 null
。