|
####简单操作过程
基本查询:
构造查询数据:
db.test.insert({name:"stephen",age:35,genda:"male",email:"stephen@hotmail.com"})
db.test.insert({name:"Stephen",age:35,genda:"male",email:"stephen@hotmail.com"})
db.test.insert({name:"stephen1",age:35,genda:"male",email:"stephen@hotmail.com"})
db.test.insert({name:"stephen",age:36,genda:"male",email:"stephen@hotmail.com"})
db.test.insert({name:"stephen",age:37,genda:"male",email:"stephen@hotmail.com"})
--多条件查询,等同于SQL语句的where name = "stephen" and age = 35
db.test.find({name:"stephen",age:35})
{ "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "stephen@hotmail.com" }
--返回指定的文档键值对,只返回name和age键值对
db.test.find({},{name:1,age:1})
{ "_id" : ObjectId("59f1ae4388f3edba94091894"), "name" : "stephen", "age" : 35 }
{ "_id" : ObjectId("59f1ae8588f3edba94091895"), "name" : "stephen", "age" : 36 }
{ "_id" : ObjectId("59f1ae8a88f3edba94091896"), "name" : "stephen", "age" : 37 }
2、查询条件
MongoDB提供了一组比较操作符:$lt/$lte/$gt/$gte/$ne,依次等价于=/!=
--返回符合条件age >= 18 && age = 18 && age n的结果,不能直接使用$size,只能是添加一个额外的键表示数据中的元素数据,在操作数据中的元素时,需要同时更新size键的值
--为后面的实验构造数据
db.wqq.update({}, {"$set": {"size":3}},false,true)
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.wqq.find()
{ "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc" ], "size" : 3 }
{ "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc" ], "size" : 3 }
{ "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc" ], "size" : 3 }
--每次添加一个新元素,都要原子性的自增size一次
db.wqq.update({},{$push:{name:123},$inc:{size:1}},0,1)
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.wqq.find()
{ "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb", "cc", 123 ], "size" : 4 }
{ "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee", "cc", 123 ], "size" : 4 }
{ "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee", "cc", 123 ], "size" : 4 }
--通过$slice返回数组中的部分数据。"$slice":2表示数组中的前两个元素
db.wqq.find({},{name:{$slice:2},size:0}) //若为负数则是最后2个元素
{ "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "aa", "bb" ] }
{ "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "aa", "ee" ] }
{ "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "ff", "ee" ] }
--$slice : [2,1],表示从第二个2元素开始取1个,如果获取数量大于2后面的元素数量,则取后面的全部数据
db.wqq.find({},{name:{$slice:[2,1]}})
db.wqq.find({},{name:{$slice:[2,1]},size:0})
{ "_id" : ObjectId("59f1c60388f3edba9409189c"), "name" : [ "cc" ] }
{ "_id" : ObjectId("59f1c6b988f3edba9409189d"), "name" : [ "cc" ] }
{ "_id" : ObjectId("59f1c6ba88f3edba9409189e"), "name" : [ "cc" ] }
6、内嵌文档查询
--当嵌入式文档为数组时,需要$elemMatch操作符来帮助定位某一个元素匹配的情况,否则嵌入式文件将进行全部的匹配
--即检索时需要将所有元素都列出来作为查询条件方可
db.aa.insert({comments:[{author:"sharesoe",score:3},{author:"mary",score:6}]})
WriteResult({ "nInserted" : 1 })
> db.aa.find()
{ "_id" : ObjectId("59f1cc8e88f3edba9409189f"), "comments" : [ { "author" : "sharesoe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }
--
db.aa.find({comments:{$elemMatch:{author:"sharesoe",score:{$gte:3}}}})
{ "_id" : ObjectId("59f1cc8e88f3edba9409189f"), "comments" : [ { "author" : "sharesoe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }
|
|