前言
最近看Mongo權(quán)威指南的時候,偶然間查了下mongo的官方文檔,發(fā)現(xiàn)其實增加了很多新的API,打算系統(tǒng)性的過一遍。
比較操作符
$eq
沒啥用
因為
db.collectionName.find({"field":{$eq:"value"}})
等于
db.collectionName.find({"field":"value"})
$gt
語法: {field: {$gt: value} }
$gte
語法: {field: {$gte: value} }
$lt
語法: {field: {$lt: value} }
$lte
語法: {field: {$lte: value} }
$in
語法: {field: {$in: [ <value1>, <value2> ... <valueN> ]} }
盡量少用$in,而是分解成一個一個的單一查詢。尤其是在分片上,$in會讓你的查詢?nèi)ッ恳粋€分片上查一次,如果實在要用的話,先在每個分片上建索引。
$ne
語法: {field: {$ne: value} }
$ne查詢會掃描整個collection, 即使是文檔上面沒有該field也會被掃描。性能會比較差
$nin
語法: { field: { $nin: [ <value1>, <value2> ... <valueN> ]} }
匹配兩種文檔
- field字段中沒有指定數(shù)組的值
- 沒有fileld字段的
查詢會全表掃描。
邏輯查詢符
邏輯操作符都是元操作符,意味著他們可以放在其他任何操作符之上
$and
語法: { $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
第一個expression應(yīng)篩去盡可能多的條件
$not
語法: { field: { $not: { <operator-expression> } } }
- $not 在大部分操作符上行為始終一致,但是在一些數(shù)據(jù)類型(比如數(shù)組)可能會產(chǎn)生不同的結(jié)果
- $not 操作符不支持 $regex操作符,但是可以使用//替代;
- $not通常不知道如何使用索引
- $not操作符與其他邏輯操作符語法有些不太一樣
$or
語法: { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
- 第一個expression1應(yīng)匹配盡量多的結(jié)果
- 使用$or查詢,每個expression可以優(yōu)先選用其自己的索引(而非符合索引)
- 除非所有的expression都對應(yīng)相應(yīng)的索引,不然$or沒有辦法使用索引
- 因為$text查詢必須使用索引,所以當(dāng)同時使用$or和$text的時候,必須所有的expression都有索引,不然會拋出錯誤
- 如果有可能,盡量用$in代替$or
$nor
語法: { $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }
- 當(dāng)使用$nor去查詢時,不僅包括不符合這個表達式的,還包括不存在于這個表達式中field字段
- 可以和$exists配合使用,例如:
db.inventory.find( { $nor: [
{ price: 1.99 }, { price: { $exists: false } },
{ sale: true }, { sale: { $exists: false } }
] } )
元素查詢操作符
$exists
語法: { field: { $exists: <boolean> } }
當(dāng)為true時,匹配包含該字段的文檔,即使該字段的值為null
- 據(jù)mongodb權(quán)威指南說,該操作符不會用到索引
$type
語法: { field: { $type: <BSON type> } },{ field: { $type: [ <BSON type1> , <BSON type2>, ... ] } }
估算查詢操作符
$expr
語法: { $expr: { <expression> } }
3.6版新增的語法,允許在查詢中使用aggregation expression
- 可以使用它比較一個文檔中的不同field的值,例如:
db.monthlyBudget.find( { $expr: { $gt: [ "$spent" , "$budget" ] } } ) - 可以使用條件語句,例如
db.supplies.find( {
$expr: {
$lt:[ {
$cond: {
if: { $gte: ["$qty", 100] },
then: { $divide: ["$price", 2] },
else: { $divide: ["$price", 4] }
}
},
5 ] }
} )
$jsonSchema
語法: { $jsonSchema: { <expression> } }
3.6版本新增語法: 講文檔與給定的JSON Schema文檔進行匹配
示例如下:
db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "gpa" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
gender: {
bsonType: "string",
description: "must be a string and is not required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
exclusiveMaximum: false,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double and is required"
}
}
}
}
})
$mod
語法: { field: { $mod: [ divisor, remainder ] } }
- 返回
字段值%divisor === remainder的文檔 - 如果數(shù)組內(nèi)元素不等于兩個會報錯
$regex
語法: { field: { $mod: [ divisor, remainder ] } }
可以使用以下的形式
{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ <field>: { $regex: 'pattern', $options: '<options>' } }
{ <field>: { $regex: /pattern/<options> } }
$text
語法:
{
$text:
{
$search: <string>,
$language: <string>,
$caseSensitive: <boolean>,
$diacriticSensitive: <boolean>
}
}
$text 只在有text索引的field上進行搜索
$where
語法: ``
- 在3.6版本中增加了$expr,與$where相比,不需要執(zhí)行javascript,所以會快很多,優(yōu)先使用$expr
- $where只能用于最頂層的文檔查詢,不能用于內(nèi)線文檔或者數(shù)組的查詢
- $where無法使用索引
示例:
db.foo.find( { $where: function() {
return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a83e994")
} } );
地理空間查詢操作符
暫時略過
數(shù)組查詢操作符
$all
語法: { <field>: { $all: [ <value1> , <value2> ... ] } }
$all查詢用于全部value的指定文檔
$elemMatch
語法: { <field>: { $elemMatch: { <query1>, <query2>, ... } } }
使用多個表達式去匹配數(shù)組的每一項,返回匹配成功的文檔
$size
語法: { <field>: { $size: length} }
匹配數(shù)組的長度,只能是固定值,不能是范圍
位查詢操作符
暫時略過
Projection操作符
$
語法:
db.collection.find( { <array>: <value> ... },
{ "<array>.$": 1 } )
db.collection.find( { <array.field>: <value> ...},
{ "<array>.$": 1 } )
返回的文檔中的數(shù)組只會保留匹配到的數(shù)組的那一項
$elemMatch
示例:
db.schools.find( { zipcode: "63109" },
{ students: { $elemMatch: { school: 102, age: { $gt: 10} } } } )
返回的文檔中會去掉不匹配的數(shù)組項,但是數(shù)組中匹配的項以及文檔的其他項還是會如期的返回
$meta
暫時略過
$slice
示例:
db.collection.find( { field: value }, { array: {$slice: count } } );
返回的數(shù)組中含有多少項
如果是正數(shù),表示返回的數(shù)組的前幾個元素
如果是負(fù)數(shù),表示返回數(shù)組的最后幾個元素