mongo CRUD資料操作符匯總,mongocrud
比較
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)。
{ <field>: { $all: [ <value1> , <value2> ... ] } }
$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: { <query> }, $comment: <comment> } )
可以理解為為每一次執行加入一條備忘,這些備忘會被記錄在profile裡,有助於後期排錯
$count
db.collection.find( { a: 5, b: 5 } ).count()
計數
$distinct
db.collection.distinct(field, query)
去重
$elemMatch
{ <field>: { $elemMatch: { <query1>, <query2>, ... } } } { _id: 1, results: [ 82, 85, 88 ] } { _id: 2, results: [ 75, 88, 89 ] } db.scores.find( { results: { $elemMatch: { $gte: 80, $lt: 85 } } } )
滿足一區間
$exists
{ field: { $exists: <boolean> } } 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: <collection-name>, query: <document>, sort: <document>, remove: <boolean>, update: <document>, new: <boolean>, fields: <document>, upsert: <boolean> }
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} );