Comparison
name |
Description |
$eq |
Equals |
$gt |
Greater than |
$gte |
Greater than or equal |
$lt |
Less than |
$lte |
Less than or equal |
$ne |
Not equal to |
$in |
In the condition |
$nin |
Not in condition |
Logic
name |
Description |
$or |
Or |
$and |
And |
$not |
Non - |
$nor |
Neither, nor, Nor:[{name: ' Bugall ', age:12}], the name is not equal to ' bugall ', age is not equal to 12 |
Elements
name |
Description |
$exists |
Whether the field exists |
$type |
field type of field |
Array
name |
Description |
$all |
Get all the data that meets the criteria |
$elemMatch |
Get all the data that meets the criteria below for a specific explanation |
$size |
If the return value is in the form of an array, define its return array element number |
Note
name |
Description |
$comment |
Add a description or remark to the command |
》
$where
"this.credits == this.debits""obj.credits == obj.debits"returnreturn 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()
Attention:
因为用了$where后mongo生成的执行计划不能很好的利用索引,所以这里不建议大家使用$where
$all
选择满足条件的所有数据(documents)。
{ <field>: { $all: [ <value1> , <value2> ... ] } }
$all的功能是和$and的功能相同的,例如$and:
{ $and"ssl""security" } ] }
$and
{ $and"ssl""security" } ] }
$batchSize
db.inventory.find().batchSize(10)
控制返回结果的数据数量(多少条数据),注意,在mongo shell里,batchSize()的设置并不会生效。同时distinct()也不会生效
$box
db.places.find( { loc: { $geoWithin: { $box: 00100100 ] ] } }} )
定义一个"盒子",2个点可以确定一个"盒子的位置"(我们把盒子理解为二维平面的矩形),那么上面这句话的意思就是找出在这个"盒子内的点",[22,22],[13,75],[19,24]这些值都将被返回
$center
db.places.find( { loc: { $geoWithin: { $center: [ [-7440.7410 ] } } })
返回所有在这个平面圆里的点(圆心坐标是(-74,40.74),圆半径是10)
$comment
db.collection.find{ $query: { <query> }, $comment: <comment> } )
可以理解为为每一次执行加入一条备注,这些备注会被记录在profile里,有助于后期排错
$count
55 } ).count()
计数
$distinct
db.collection.distinct(field, query)
去重
$elemMatch
{ <field>: { $elemMatch: { <query1>, <query2>, ... } } } 1828588 ] } 2758889 ] } db.scores.find( 8085 } } } )
满足一区间
$exists
{field: {$exists: <boolean>}} db.records.find ({A: {$exists: true}}) Result: {A:5B:5, c:null} {a:3, B:null, C:8} {a:null, B:3C:9} {A:1B:2C:3} {A:2C:5} {A:3B:2} {A:4} db.records.find ({b: {$exists: false}}) Result: {A:2C:5} {A:4} {c:6}
$find
‘Los Pollos Hermanos‘ }).find(callback)
查找所有满足条件的数据
$findOne
‘Los Pollos Hermanos‘ }).find(callback)
返回一个满足条件的数据
$findAndModify
{ findAndModify: <collection-name>, query: <document>, sort: <document>, remove: <boolean>, update: <document>, new: <boolean>, fields: <document>, upsert: <boolean> }
Field |
Description |
Query |
a normal request statement may get more than one piece of data, but findandmodify will only process the first data |
sort |
because findandmodify can handle only one piece of data, we usually sort the data we get from query |
Remove |
If True removes the first data after query and defaults to False |
update |
update the first article after query |
New |
When set to True, returns the modified data instead of the original data, which defaults to False |
fields |
setting group |
Upsert |
When the value is true, the updated values are updated if they exist and are inserted if they do not exist. The default is False |
db.people.findAndModify( { "people", "Gus""active"100 }, 1 }, 1 } }, upsert: true, new : true} );
MONGO CRUD Data Operators summary