MongoDB query (3) - embedded document query (7)

MongoDB query embedded document

Please reprint from the source: http://eksliang.iteye.com/blog/2177301

I. Overview

       There are two ways to query embedded documents: query the entire document; query against key-value pairs. These two methods are different, and I will illustrate them separately with examples below.

 

2. Query the entire document

For example: there are the following documents

db.emp.insert({
	"id":"A001",
	"name":{
		"first":"Carey",
		"last":"Ickes"
	},
	"age":25
})

 Reference example: People whose query name is equal to "Carey Ickes" can query like this

 

db.emp.find({"name":{"first":"Carey","last":"Ickes"}})
        This query will match the entire embedded document exactly. If Carey decides to add a middle name key, then this query will not work because the query condition does not match the entire embedded document. And this kind of query is also related to the order. If the query condition is changed to {"last":"Ickes","first":"Carey"}, nothing will be matched.

 

 

3. Key-value pair query

When we query, we generally do not match the entire embedded document, and usually only query for specific key values ​​of the embedded document. How to do it?

A: When querying a document, you can use "." to indicate entering the embedded document.

Reference example:

 

> db.emp.find({"name.last":"Ickes","name.first":"Carey"})
Now, if Ickes adds more keys, the query will still match his first and last name

 

 

 

 Fourth, the query that contains embedded documents in the array

This kind of query is relatively complex, so matching of embedded documents also requires some skill. For example, there is a comments: key in the following blog document to save other people's comment information.

 

db.blog.insert({
	"_id":"B001",
	"title":"MongoDB查询",
	"comments":[
	  {"name":"ickes","score":3,"comment":"nice"},
	  {"name":"xl","score":4,"comment":"nice"},
	  {"name":"eksliang","score":5,"comment":"nice"},
	  {"name":"ickes","score":6,"comment":"nice"}
	]
})
 Now query for articles commented by ickes with a score of 5 or more

 

  1. 不能使用db.blog.find({"comments":{"name":"ickes","score":{"$gt":5}})去查,因为内嵌文档的匹配是精确匹配,必须要匹配完整的文档,而这个查询不会匹配comment键
  2. 不能使用db.blog.find({"comments":{"name":"ickes","score":{"$gt":5},"comment":"nice"}})去查,还是那句话,文档的匹配时精确匹配,这里使用了$gt作为范围,所以也查不到
  3. 不能使用db.blog.find({"comments.name":"ickes","comments.score":{"$gt":5}})去查,前面讲查询条件的时候说过,查询条件里面的键值对会解释为AND,但是对于数组的内嵌文档他会解释为OR的关系,也就是说上面实际是这样的comments.name:ickes或者comments.score":{"$gt":5},这明显不行吗!(注意如果内嵌文档不在数组中,还是AND,所以我才把这个拿出来单独讨论)

 

那对于数组里面的内嵌文档到底怎么办?应该这么办,如下所示

这里需要使用"$elemMatch"操作符,仅当这种时候才使用这个操作符

参考实例:

db.blog.find({"comments":{
	"$elemMatch":{"name":"ickes","score":{"$gt":5}}
}})

 

 

五、返回与查询条件相匹配的任意一个数组元素

       我们可以使用"$slice"操作符进行数组元素返回限制,但是当数组里面保存的是文档的时候,我就想返回与我查询条件相匹配的那个元素,其他的不要,怎么做?有技巧的哦!

文档结构如下:

db.blog.insert({
	"_id":"B001",
	"title":"MongoDB查询",
	"comments":[
	  {"name":"ickes","score":3,"comment":"nice"},
	  {"name":"xl","score":4,"comment":"nice"},
	  {"name":"eksliang","score":5,"comment":"nice"},
	  {"name":"ickes","score":6,"comment":"nice"}
	]
})

 

参考实例:

db.blog.find({"comments":{
	"$elemMatch":{"name":"ickes","score":{"$gt":5}}}},
	{"comments.$":1}--第二个参数是限制返回数据的,别看错了,这是第二个参数
)

 返回结果如下:仅返回与当前查询条件相匹配的那个内嵌文档。

{
  "_id" : "B001", 
  "comments" : [ { "name" : "ickes", "score" : 6, "comment" : "nice" } ] 
}

 如果当前查询有多个内嵌文档匹配,只会返回第一个。

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326428228&siteId=291194637