mongodb操作符

时间:2022-05-17 14:57:18

1."$gt" 、"$gte"、 "$lt"、 "$lte"、"null查询"、"$all"、"$size"、"$in"、"$nin"、"$and"、"$nor"、"$not"、"$or"、"$exists"、"$mod"、"$regex"、"$where"、"$slice"、"$elemMatch"

2.要是想查询数组指定位置的元素,则需使用key.index语法指定下标

3. $and  :    db.getCollection('students').find({ $and: [ { name: "t1" }, { amount: { $lt:50 } } ] } )

4. $nor :       db.getCollection('students').find( { $nor: [ { name: "t1" }, { qty: { $lt: 50 } } ] } )

5. $or :  执行逻辑OR运算,指定一个至少包含两个表达式的数组,选择出至少满足数组中一条表达式的文

db.getCollection('students').find( { $or: [ { amount: { $gt: 50 } }, { name: "t1" } ] } )

6.$exists:查询amount字段存在,且值不等于16和58的文档

db.getCollection('students').find( { amount: { $exists: true, $nin: [ 16, 58 ] } } )

7.$regex

db.getCollection('students').find( { name: /a/i } )

8.$elemMatch

$elemMatch可以指定多个字段的限定条件,下面的操作将查询邮政编码键值是63109的所有文档。 $elemMatch操作符将返回 students数组中的第一个匹配条件(内嵌文档的school键且值为102且age键值大于10)的元素

db.school.find( { zipcode: 63109 },{ students: { $elemMatch: { school: 102, age: { $gt: 10} } } } );