比较
| name | description |
|---|---|
| $eq | 等于 |
| $gt | 大于 |
| $gte | 大于等于 |
| $lt | 小于 |
| $lte | 小于等于 |
| $ne | 不等于 |
| $in | 在条件中 |
| $nin | 不在条件中 |
逻辑
| name | description |
|---|---|
| $or | 或者 |
| $and | 且 |
| $not | 非 |
| $nor | 既不也不,nor:[{name:’bugall’,age:12}],名字不等于’bugall’,age不等于12 |
元素
| name | description |
|---|---|
| $exists | 字段是否存在 |
| $type | 字段的字段类型 |
数组
| name | description |
|---|---|
| $all | 获取所有满足条件的数据 |
| $elemMatch | 获取所有满足条件的数据,下面有具体讲解 |
| $size | 如果是数组形式的返回值,定义其返回数组元素个数 |
备注
| name | description |
|---|---|
| $comment | 给命令加上一条描述或是备注 |
》
$where
db.myCollection.find( { $where: "this.credits == this.debits" } );
db.myCollection.find( { $where: "obj.credits == obj.debits" } );
db.myCollection.find( { $where: function() { return (this.credits == this.debits) } } );
db.myCollection.find( { $where: function() { return obj.credits == obj.debits; } } );
where接受字符串函数两种形式的参数,其中也提供了一些函数。
assert()
BinData()
DBPointer()
DBRef()
doassert()
emit()
gc()
HexData()
hex_md5()
isNumber()
isObject()
ISODate()
isString()
Map()
MD5()
NumberInt()
NumberLong()
ObjectId()
print()
printjson()
printjsononeline()
sleep()
Timestamp()
tojson()
tojsononeline()
tojsonObject()
UUID()
version()
注意:
因为用了$where后mongo生成的执行计划不能很好的利用索引,所以这里不建议大家使用$where
$all
选择满足条件的所有数据(documents)。
{ : { $all: [ , ... ] } }
$all的功能是和$and的功能相同的,例如$and:
{ $and: [ { tags: "ssl" }, { tags: "security" } ] }
$and
{ $and: [ { tags: "ssl" }, { tags: "security" } ] }
$batchSize
db.inventory.find().batchSize(10)
控制返回结果的数据数量(多少条数据),注意,在mongo shell里,batchSize()的设置并不会生效。同时distinct()也不会生效
$box
db.places.find( {
loc: { $geoWithin: { $box: [ [ 0, 0 ], [ 100, 100 ] ] } }
} )
定义一个"盒子",2个点可以确定一个"盒子的位置"(我们把盒子理解为二维平面的矩形),那么
上面这句话的意思就是找出在这个"盒子内的点",[22,22],[13,75],[19,24]这些值都将被
返回
$center
db.places.find(
{ loc: { $geoWithin: { $center: [ [-74, 40.74], 10 ] } } }
)
返回所有在这个平面圆里的点(圆心坐标是(-74,40.74),圆半径是10)
$comment
db.collection.find( { $query: { }, $comment: } )
可以理解为为每一次执行加入一条备注,这些备注会被记录在profile里,有助于后期排错
$count
db.collection.find( { a: 5, b: 5 } ).count()
计数
$distinct
db.collection.distinct(field, query)
去重
$elemMatch
{ : { $elemMatch: { , , ... } } }
{ _id: 1, results: [ 82, 85, 88 ] }
{ _id: 2, results: [ 75, 88, 89 ] }
db.scores.find(
{ results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)
满足一区间
$exists
{ field: { $exists: } }
db.records.find( { a: { $exists: true } } )
result:
{ a: 5, b: 5, c: null }
{ a: 3, b: null, c: 8 }
{ a: null, b: 3, c: 9 }
{ a: 1, b: 2, c: 3 }
{ a: 2, c: 5 }
{ a: 3, b: 2 }
{ a: 4 }
db.records.find( { b: { $exists: false } } )
result:
{ a: 2, c: 5 }
{ a: 4 }
{ c: 6 }
$find
query.find({ name: 'Los Pollos Hermanos' }).find(callback)
查找所有满足条件的数据
$findOne
query.findOne({ name: 'Los Pollos Hermanos' }).find(callback)
返回一个满足条件的数据
$findAndModify
{
findAndModify: ,
query: ,
sort: ,
remove: ,
update: ,
new: ,
fields: ,
upsert:
}
Field
Description
query
一条普通的请求语句,可能会得到多条数据,但是findAndModify只会处理第一条数据
sort
因为findAndModify只能处理一条数据,所以通常我们会把query得到的数据进行排序
remove
如果为true则移除query后的第一条数据,默认为false
update
更新query后的第一条
new
当被设置为true的时候,则返回修改后的数据而不是原始数据,默认为false
fields
设置分组
upsert
当值为true时,更新的值如果存在则更新,如果不存在则插入。默认为false
db.people.findAndModify( {
findAndModify: "people",
query: { name: "Gus", state: "active", rating: 100 },
sort: { rating: 1 },
update: { $inc: { score: 1 } },
upsert: true,
new : true
} );