Mongodb内嵌文档的查询
来源:互联网 发布:怎样让淘宝流量上去 编辑:程序博客网 时间:2024/05/22 14:12
在《MongoDB权威指南》这本书中4.3.4 中有个查询文档的章节,里面用到了find和elemMatch,但是给出的例子不是很详细,所以理解起来有点困难,我们来用例子来演示下。
0.目的
为了在blog集合中找到由Joe发表的5分以上的评论
1.表结构如下
> db.blog.findOne(){ "_id" : ObjectId("55814703e6c873d825fdcc1a"), "content" : "...", "conments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "joe", "score" : 6, "comment" : "terrible post" } ]}
2.构建测试样例
db.blog.insert({"content":"...", "conments":[ { "author":"joe", "score":3, "comment":"nice post" }, { "author":"joe", "score":6, "comment":"terrible post" } ] })db.blog.insert({"content":"...", "conments":[ { "author":"joee", "score":3, "comment":"nice post" }, { "author":"joee", "score":6, "comment":"terrible post" } ] })db.blog.insert({"content":"...", "conments":[ { "author":"joe", "score":3, "comment":"nice post" }, { "author":"joee", "score":6, "comment":"terrible post" } ] })db.blog.insert({"content":"...", "conments":[ { "author":"joee", "score":3, "comment":"nice post" }, { "author":"joe", "score":6, "comment":"terrible post" } ] })
> db.blog.find(){ "_id" : ObjectId("55814703e6c873d825fdcc1a"), "content" : "...", "conments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "joe", "score" : 6, "comment" : "terrible post" } ]}{ "_id" : ObjectId("55814711e6c873d825fdcc1b"), "content" : "...", "conments" : [ { "author" : "joee", "score" : 3, "comment" : "nice post" }, { "author" : "joee", "score" : 6, "comment" : "terrible post" } ]}{ "_id" : ObjectId("55814719e6c873d825fdcc1c"), "content" : "...", "conments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "joee", "score" : 6, "comment" : "terrible post" } ]}{ "_id" : ObjectId("55814725e6c873d825fdcc1d"), "content" : "...", "conments" : [ { "author" : "joee", "score" : 3, "comment" : "nice post" }, { "author" : "joe", "score" : 6, "comment" : "terrible post" } ]}
3.测试1
db.blog.find({"comments" : {"author" : "joe", "score": {"$gte" : 5 }}})
没有输出任何内容,所以不正确。
4.测试2
db.blog.find({“conments.author”:”joe”, “conments.score”:{“$gte”:5}})
结果如下:
db.blog.find({"conments.author":"joe", "conments.score":{"$gte":5}}){ "_id" : ObjectId("55814703e6c873d825fdcc1a"), "content" : "...", "conments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "joe", "score" : 6, "comment" : "terrible post" } ]}{ "_id" : ObjectId("55814719e6c873d825fdcc1c"), "content" : "...", "conments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "joee", "score" : 6, "comment" : "terrible post" } ]}{ "_id" : ObjectId("55814725e6c873d825fdcc1d"), "content" : "...", "conments" : [ { "author" : "joee", "score" : 3, "comment" : "nice post" }, { "author" : "joe", "score" : 6, "comment" : "terrible post" } ]}
但是你会发现第二条文档并不满足条件也给find出来了。
5.正确做法
db.blog.find({"conments": { "$elemMatch" : { "author" : "joe", "score": {"$gte": 5} }}} )
结果如下:
{ "_id" : ObjectId("55814703e6c873d825fdcc1a"), "content" : "...", "conments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "joe", "score" : 6, "comment" : "terrible post" } ]}{ "_id" : ObjectId("55814725e6c873d825fdcc1d"), "content" : "...", "conments" : [ { "author" : "joee", "score" : 3, "comment" : "nice post" }, { "author" : "joe", "score" : 6, "comment" : "terrible post" } ]}
0 0
- Mongodb内嵌文档的查询
- mongodb查询内嵌文档
- mongodb查询内嵌文档
- MongoDB查询内嵌文档
- MongoDB内嵌文档查询
- mongodb 查询内嵌文档
- mongoDB内嵌文档查询
- MongoDB查询内嵌文档
- MongoDB 内嵌文档查询
- mongodb数组与内嵌文档查询
- MongoDB 内嵌文档
- MongoDB查询(数组、内嵌文档和$where)
- mongodb 内嵌文档多条件查询($elemMatch)
- MongoDB查询(数组、内嵌文档和$where)
- mongodb中根据内嵌文档中的条件查询
- MongoDB查询(数组、内嵌文档和$where)
- 关于 mongodb 的 内嵌关联查询
- 学习MongoDB--(4-2):MongoDB查询(数组、内嵌文档和$where)
- 网络通信中--大小端问题
- CentOS下nginx+mysql+php环境安装,小结
- C++ 无法解析的外部符号
- ThinkPHP3.2.3 实现项目分组
- AsyncTask异步任务
- Mongodb内嵌文档的查询
- Android学习 10-> 单帧布局
- Facebook推出强大Android图片库Fresco
- C# 中的委托和事件
- Android Activity life Cycle
- 4 Median of Two Sorted Arrays
- Akka学习笔记-简介与API简单操作
- 安卓打造listview的万用adapter
- 大文件,5亿整数,怎么排?