这节来说说如何检索mongodb数据。首先向文档中插入一些数据。
1. 插入数据
代码如下> use ttlsa_com
switched to db ttlsa_com
> db.mediaCollection.insert({ "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author": [ "Membrey, Peter", "Plugge, Eelco", "Hawkins, Tim" ] })
> db.mediaCollection.insert({ "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" })
> db.mediaCollection.insert({ "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ]})
> db.mediaCollection.find()
{ "_id" : ObjectId("5353462f93efef02c962da71"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author" : [ "Membrey, Peter", "Plugge, Eelco", "Hawkins, Tim" ] }
{ "_id" : ObjectId("5353462f93efef02c962da72"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" }
{ "_id" : ObjectId("5353463193efef02c962da73"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }
2. 检索
find函数是经常用到的一个。前面的文章也有介绍到。下面看看有选择性的检索,查看你感兴趣的数据。
检索”Artist” : “Nirvana”的数据:
代码如下> db.mediaCollection.find({"Artist" : "Nirvana"}).toArray()
[
{
"_id" : ObjectId("5353462f93efef02c962da72"),
"Type" : "CD",
"Artist" : "Nirvana",
"Title" : "Nevermind"
},
{
"_id" : ObjectId("5353463193efef02c962da73"),
"Type" : "CD",
"Artist" : "Nirvana",
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : "1",
"Title" : "Smells like teen spirit",
"Length" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
}
]
上面的查询虽说检索出”Artist” : “Nirvana”的数据,但是返回了全部列的信息,但是我只要查看Title和Tracklist.Title列
代码如下> db.mediaCollection.find({"Artist" : "Nirvana"}, {Title:1, "Tracklist.Title":1}).toArray()
[
{
"_id" : ObjectId("5353462f93efef02c962da72"),
"Title" : "Nevermind"
},
{
"_id" : ObjectId("5353463193efef02c962da73"),
"Title" : "Nevermind",
"Tracklist" : [
{
"Title" : "Smells like teen spirit"
},
{
"Title" : "In Bloom"
}
]
}
]
Title:1, “Tracklist.Title”:1表示只返回这两列信息。升序。也可以反着来Title:0, “Tracklist.Title”:0表示返回除了这两列的其他所有列信息。
注意:_id字段总是会返回。
3. 使用逗号
当文档结构变的复杂时,如含有数组或嵌入对象文档,就需要使用到逗号,来检索嵌入在文档中的信息。
代码如下> db.mediaCollection.find({"Tracklist.Length":"5:02"}).toArray()
[
{
"_id" : ObjectId("5353463193efef02c962da73"),
"Type" : "CD",
"Artist" : "Nirvana",
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : "1",
"Title" : "Smells like teen spirit",
"Length" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
}
]
查询整个内嵌文档:
代码如下> db.mediaCollection.find({Tracklist:{"Length":"5:02"}}).toArray()
[ ]
> db.mediaCollection.find({Tracklist:{"Track" : "1","Title" : "Smells like teen spirit","Length":"5:02"}}).toArray()
[
{
"_id" : ObjectId("5353463193efef02c962da73"),
"Type" : "CD",
"Artist" : "Nirvana",
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : "1",
"Title" : "Smells like teen spirit",
"Length" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
}
]
> db.mediaCollection.find({Tracklist:{"Track" : "1","Length" : "5:02","Title" : "Smells like teen spirit"}}).toArray()
[ ]
查询整个文档需要全部列出内嵌文档的字段,且顺序要一致,否则匹配不到。
查询内嵌文档的多个字段。如查询有joe发表且分数在5分以上:
代码如下> db.mediaCollection.insert({ "content" : "...", "comments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "mary", "score" : 6, "comment" : "terrible post" } ] })
> db.mediaCollection.find().toArray()
[
{
"_id" : ObjectId("5353462f93efef02c962da71"),
"Type" : "Book",
"Title" : "Definitive Guide to MongoDB, the",
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim"
]
},
{
"_id" : ObjectId("5353462f93efef02c962da72"),
"Type" : "CD",
"Artist" : "Nirvana",
"Title" : "Nevermind"
},
{
"_id" : ObjectId("5353463193efef02c962da73"),
"Type" : "CD",
"Artist" : "Nirvana",
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : "1",
"Title" : "Smells like teen spirit",
"Length" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
},
{
"_id" : ObjectId("5353681293efef02c962da7a"),
"content" : "...",
"comments" : [
{
"author" : "joe",
"score" : 3,
"comment" : "nice post"
},
{
"author" : "mary",
"score" : 6,
"comment" : "terrible post"
}
]
}
]
> db.mediaCollection.find({"comments" : {"author" : "joe", "score" : {"$gte" : 5}}}).toArray()
[ ]
> db.mediaCollection.find({"comments.author" : "joe", "comments.score" : {"$gte" : 5}}).toArray()
[
{
"_id" : ObjectId("5353681293efef02c962da7a"),
"content" : "...",
"comments" : [
{
"author" : "joe",
"score" : 3,
"comment" : "nice post"
},
{
"author" : "mary",
"score" : 6,
"comment" : "terrible post"
}
]
}
]
上面的查询是不对的。
要正确的指定一组条件,而不是每个键,因此要使用到$elemMatch。这样就可以用来部分指定匹配数组中的单个内嵌文档的限定条件。正确的写法如下所示:
代码如下> db.mediaCollection.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}).toArray()
[ ]
对于数组:
代码如下> db.mediaCollection.find({"Author":"Membrey, Peter"}).toArray()
[
{
"_id" : ObjectId("5353462f93efef02c962da71"),
"Type" : "Book",
"Title" : "Definitive Guide to MongoDB, the",
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim"
]
}
]
正则表达式查询:
代码如下> db.mediaCollection.find({"Title":/MongoDB/i}).toArray()
[
{
"_id" : ObjectId("5353462f93efef02c962da71"),
"Type" : "Book",
"Title" : "Definitive Guide to MongoDB, the",
"ISBN" : "987-1-4302-3051-9",
"Publisher" : "Apress",
"Author" : [
"Membrey, Peter",
"Plugge, Eelco",
"Hawkins, Tim"
]
}
]
对检索结果进行Sort, Limit, 和Skip请看下节内容。
下面是附加整理查询语句
1. 基本查询:
构造查询数据。
> db.test.findOne()
{
"_id" : ObjectId("4fd58ecbb9ac507e96276f1a"),
"name" : "stephen",
"age" : 35,
"genda" : "male",
"email" : "www.111cn.net"
}
--多条件查询。下面的示例等同于SQL语句的where name = "stephen" and age = 35
> db.test.find({"name":"stephen","age":35})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "www.111cn.net" }
--返回指定的文档键值对。下面的示例将只是返回name和age键值对。
> db.test.find({}, {"name":1,"age":1})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35 }
--指定不返回的文档键值对。下面的示例将返回除name之外的所有键值对。
> db.test.find({}, {"name":0})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "age" : 35, "genda" : "male", "email" : "www.111cn.net" }
2. 查询条件:
MongoDB提供了一组比较操作符:$lt/$lte/$gt/$gte/$ne,依次等价于<=/>/>=/!=。
--下面的示例返回符合条件age >= 18 && age <= 40的文档。
> db.test.find({"age":{"$gte":18, "$lte":40}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "www.111cn.net" }
--下面的示例返回条件符合name != "stephen1"
> db.test.find({"name":{"$ne":"stephen1"}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "www.111cn.net" }
--$in等同于SQL中的in,下面的示例等同于SQL中的in ("stephen","stephen1")
> db.test.find({"name":{"$in":["stephen","stephen1"]}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "www.111cn.net" }
--和SQL不同的是,MongoDB的in list中的数据可以是不同类型。这种情况可用于不同类型的别名场景。
> db.test.find({"name":{"$in":["stephen",123]}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "www.111cn.net" }
--$nin等同于SQL中的not in,同时也是$in的取反。如:
> db.test.find({"name":{"$nin":["stephen2","stephen1"]}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "www.111cn.net" }
--$or等同于SQL中的or,$or所针对的条件被放到一个数组中,每个数组元素表示or的一个条件。
--下面的示例等同于name = "stephen1" or age = 35
> db.test.find({"$or": [{"name":"stephen1"}, {"age":35}]})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "www.111cn.net" }
--下面的示例演示了如何混合使用$or和$in。
> db.test.find({"$or": [{"name":{"$in":["stephen","stephen1"]}}, {"age":36}]})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "www.111cn.net" }
--$not表示取反,等同于SQL中的not。
> db.test.find({"name": {"$not": {"$in":["stephen2","stephen1"]}}})
{ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "www.111cn.net" }
3. null数据类型的查询:
代码如下 --在进行值为null数据的查询时,所有值为null,以及不包含指定键的文档均会被检索出来。> db.test.find({"x":null})
{ "_id" : ObjectId("4fd59d30b9ac507e96276f1b"), "x" : null }
{ "_id" : ObjectId("4fd59d49b9ac507e96276f1c"), "y" : 1 }
--需要将null作为数组中的一个元素进行相等性判断,即便这个数组中只有一个元素。
--再有就是通过$exists判断指定键是否存在。
> db.test.find({"x": {"$in": [null], "$exists":true}})
{ "_id" : ObjectId("4fd59d30b9ac507e96276f1b"), "x" : null }
4. 正则查询:
> db.test.find()
{ "_id" : ObjectId("4fd59ed7b9ac507e96276f1d"), "name" : "stephen" }
{ "_id" : ObjectId("4fd59edbb9ac507e96276f1e"), "name" : "stephen1" }
--i表示忽略大小写
> db.test.find({"name":/stephen?/i})
{ "_id" : ObjectId("4fd59ed7b9ac507e96276f1d"), "name" : "stephen" }
{ "_id" : ObjectId("4fd59edbb9ac507e96276f1e"), "name" : "stephen1" }
5. 数组数据查询:
--基于数组的查找。
{ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat","orange" ] }
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] }
--数组中所有包含banana的文档都会被检索出来。
> db.test.find({"fruit":"banana"})
{ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] }
--检索数组中需要包含多个元素的情况,这里使用$all。下面的示例中,数组中必须同时包含apple和banana,但是他们的顺序无关紧要。
> db.test.find({"fruit": {"$all": ["banana","apple"]}})
{ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana", "apple" ] }
--下面的示例表示精确匹配,即被检索出来的文档,fruit值中的数组数据必须和查询条件完全匹配,即不能多,也不能少,顺序也必须保持一致。
> db.test.find({"fruit":["apple","banana","peach"]})
{ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", peach" ] }
--下面的示例将匹配数组中指定下标元素的值。数组的起始下标是0。
> db.test.find({"fruit.2":"peach"})
{ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", peach" ] }
--可以通过$size获取数组的长度,但是$size不能和比较操作符联合使用。
> db.test.find({"fruit": {$size : 3}})
{ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }
{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat","orange" ] }
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] }
--如果需要检索size > n的结果,不能直接使用$size,只能是添加一个额外的键表示数据中的元素数据,在操作数据中的元素时,需要同时更新size键的值。
--为后面的实验构造数据。
> db.test.update({}, {"$set": {"size":3}},false,true)
> db.test.find()
{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat", "orange" ], "size" : 3 }
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana", "apple" ], "size" : 3 }
--每次添加一个新元素,都要原子性的自增size一次。
> test.update({},{"$push": {"fruit":"strawberry"},"$inc":{"size":1}},false,true)
> db.test.find()
{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat", "orange", "strawberry" ], "size" : 4 }
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana", "apple", "strawberry" ], "size" : 4 }
--通过$slice返回数组中的部分数据。"$slice":2表示数组中的前两个元素。
> db.test.find({},{"fruit": {"$slice":2}, "size":0})
{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat" ]}
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana" ]}
--通过$slice返回数组中的部分数据。"$slice":-2表示数组中的后两个元素。
> db.test.find({},{"fruit": {"$slice":-2}, "size":0})
{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "orange", "strawberry" ] }
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "apple", "strawberry" ] }
--$slice : [2,1],表示从第二个2元素开始取1个,如果获取数量大于2后面的元素数量,则取后面的全部数据。
> db.test.find({},{"fruit": {"$slice":[2,1]}, "size":0})
{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "orange" ] }
{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "apple" ] }
6. 内嵌文档查询:
> db.test.find()
{ "_id" : ObjectId("4fd5ada3b9ac507e96276f22"), "name" : { "first" : "Joe", "last" : "He" }, "age" : 45 }
--当嵌入式文档为数组时,需要$elemMatch操作符来帮助定位某一个元素匹配的情况,否则嵌入式文件将进行全部的匹配。
--即检索时需要将所有元素都列出来作为查询条件方可。
> db.test.findOne()
{
"_id" : ObjectId("4fd5af76b9ac507e96276f23"),
"comments" : [
{
"author" : "joe",
"score" : 3
},
{
"author" : "mary",
"score" : 6
}
]
}
> db.test.find({"comments": {"$elemMatch": {"author":"joe","score":{"$gte":3}}}}
{ "_id" : ObjectId("4fd5af76b9ac507e96276f23"), "comments" : [ { "author" : "joe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }
7. 游标:
数据库使用游标来返回find()的执行结果,客户端对游标可以进行有效的控制,如:限定结果集的数量、跳过部分结果、基于任意键的任意方向的排序等。
下面的例子将用于准备测试数据。
> for (i = 0; i < 10; ++i) {
... db.testtable.insert({x:i})
... }
我们可以通过cursor提供的hasNext()方法判断是否还有未读取的数据,再通过next()方法读取结果集中的下一个文档。如:
代码如下 > var c = db.testtable.find()> while (c.hasNext()) {
... print(c.next().x)
... }
当调用find()的时候,shell并不立即查询数据库,而是等待真正开始要求获得结果的时候才发送查询,这样在执行之前可以给查询附加额外的选项。几乎所有的游标方法都返回本身,因此可以像下面这样将游标的方法链式组合起来。如:
代码如下 > var c1 = db.testtable.find().sort({"x":1}).limit(1).skip(4);> var c2 = db.testtable.find().limit(1).sort({"x":1}).skip(4);
> var c3 = db.testtable.find().skip(4).limit(1).sort({"x":1});
此时,查询并未执行,所有这些函数都是在构造查询,当执行下面的语句时,查询将被真正执行,
代码如下 > c.hasNext()查询被发送到服务器,MongoDB服务器每次将返回一批数据,当本批被全部迭代后再从服务器读取下一批数据,直至查询结果需要的数据被全部迭代
【mongodb数据常用查询语句】mongodb数据常用查询语句笔记
http://m.bbyears.com/shujuku/53278.html
推荐访问: