MongoDB进阶
1、$type操作符
用途:使用MongoDB时,在需要根据字段的类型来查询数据时,可以使用$type操作符来完成。
语法:db.collection.find({字段:{$type:类型}})
其中,类型的值可以使用以下列出的
Type | Number | Alias | Notes |
Double | 1 | “double” | |
String | 2 | “string” | |
Object | 3 | “object” | |
Array | 4 | “array” | |
Binary data | 5 | “binData” | |
Undefined | 6 | “undefined” | Deprecated. |
ObjectId | 7 | “objectId” | |
Boolean | 8 | “bool” | |
Date | 9 | “date” | |
Null | 10 | “null” | |
Regular Expression | 11 | “regex” | |
DBPointer | 12 | “dbPointer” | Deprecated. |
JavaScript | 13 | “javascript” | |
Symbol | 14 | “symbol” | Deprecated. |
JavaScript (with scope) | 15 | “javascriptWithScope” | |
32-bit integer | 16 | “int” | |
Timestamp | 17 | “timestamp” | |
64-bit integer | 18 | “long” | |
Decimal128 | 19 | “decimal” | New in version 3.4. |
Min key | -1 | “minKey” | |
Max key | 127 | “maxKey” |
示例:
db.my_collection.insert({"name":"rlxy93","age":21})
WriteResult({ "nInserted" : 1 })
> db.my_collection.insert({"name":"lxy","age":22})
WriteResult({ "nInserted" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 21 }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
> db.my_collection.find({age:{$type:"double"}})
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 21 }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
2、条件操作符
用途:在查询过程中,需要大于某个值或小于某个值。
语法:db.collection.find({字段:{条件操作符:值}})
条件操作符可取的值有
(>) 大于 - $gt
(<) 小于 - $lt
(>=) 大于等于 - $gte
(<= ) 小于等于 - $lte
示例:
> db.my_collection.find({age:{$gt:21}})
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
3、聚合函数
用途:在查询过程中,需要获取某一列的总和,平均值等。
介绍:基本聚合函数有三种:count,distinct和group。
count
用途:统计符合条件的文档的数量。
语法:db.collection.count(query)或db.collection.find(query).count()
示例:
> db.my_collection.find({age:{$gt:20}})
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 21 }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
> db.my_collection.find({age:{$gt:20}}).count()
2
distinct
用途:去除查询后的重复文档。
语法:db.collection.distinct(field,query)
示例:
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 21 }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
> db.my_collection.distinct("age",{age:{$gt:20}})
[ 21, 22 ]
4、修改器
用途:用于更新操作。
取值:$inc,$set,$unset,$push,$pop,$pull,$upsert。
$inc
用途:对数值类型进行自增自减某个值。
示例:
> db.my_collection.update({"name":"rlxy93"},{$inc:{"age":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 22 }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
> db.my_collection.update({"name":"rlxy93"},{$inc:{"age":3}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25 }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
$set
用途:指定键并更新键值,如果键不存在则创建。
示例:
> db.my_collection.update({"name":"rlxy93"},{$set:{"address":"chongqing"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "chongqing" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
> db.my_collection.update({"name":"rlxy93"},{$set:{"address":"sichuan"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
#更改值中的键值,需要使用.连接符
> db.my_collection.insert({"name":"a","age":20,"list":{"a":"a","b":"b","c":"c"}})
WriteResult({ "nInserted" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : { "a" : "a", "b" : "b", "c" : "c" } }
> db.my_collection.update({"name":"a"},{$set:{"list.a":"A"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : { "a" : "A", "b" : "b", "c" : "c" } }
$unset
用途:删除键。
示例:
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : { "a" : "A", "b" : "b", "c" : "c" } }
> db.my_collection.update({"name":"a"},{$unset:{"list":-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20 }
$push
用途:向数组类型的键里面添加一个数组元素。
示例:
> db.my_collection.update({"name":"a"},{$push:{"list":0}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : [ 0 ] }
> db.my_collection.update({"name":"a"},{$push:{"list":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : [ 0, 1 ] }
$pop
用途:将数组类型的键值里面从头或者尾删除元素。
示例:
> db.my_collection.update({"name":"a"},{$pop:{"list":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : [ 0, 1 ] }
> db.my_collection.update({"name":"a"},{$pop:{"list":-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : [ 1 ] }
$pull
用途:删除满足条件的数组中的元素。
示例:
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : [ 1, 0, 2, 3, 4 ] }
> db.my_collection.update({"name":"a"},{$pull:{"list":3}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.my_collection.find()
{ "_id" : ObjectId("5dec9fe3a849a105badc5d27"), "name" : "rlxy93", "age" : 25, "address" : "sichuan" }
{ "_id" : ObjectId("5dec9feda849a105badc5d28"), "name" : "lxy", "age" : 22 }
{ "_id" : ObjectId("5decaa9968bcf6e521e4d3aa"), "name" : "l", "age" : 21 }
{ "_id" : ObjectId("5decb1ad68bcf6e521e4d3ad"), "name" : "a", "age" : 20, "list" : [ 1, 0, 2, 4 ] }
$upsert
用途:upsert是一种特殊的更新,如果没有符合条件的文档,就以upsert设置的条件来创建一个新的文档,如果有,则在已有的文档上进行更新。
示例:
> db.my_collection.update({"name":"b"},{$inc:{"age":22}},true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5decb7e26f15b1488bba141a")
})
> db.my_collection.find()
{ "_id" : ObjectId("5decb7e26f15b1488bba141a"), "name" : "b", "age" : 22 }