Mongodb嵌套数组搜索

编程入门 行业动态 更新时间:2024-10-28 09:27:57
本文介绍了Mongodb嵌套数组搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

文档结构示例为:

{ "dob": "12-13-2001", "name": "Kam", "visits": { "0": { "service_date": "12-5-2011", "payment": "40", "chk_number": "1234455", }, "1": { "service_date": "12-15-2011", "payment": "45", "chk_number": "3461234", }, "2": { "service_date": "12-25-2011", "payment": "25", "chk_number": "9821234", } } } { "dob": "10-01-1998", "name": "Sam", "visits": { "0": { "service_date": "12-5-2011", "payment": "30", "chk_number": "86786464", }, "1": { "service_date": "12-15-2011", "payment": "35", "chk_number": "45643461234", }, "2": { "service_date": "12-25-2011", "payment": "20", "chk_number": "4569821234", } } }

在PHP中,我想列出所有付款少于"30"的访问"信息(和相应的名称").

In PHP i want to list all those "visits" information (and corresponding "name" ) for which payment is less than "30".

我只想打印付款"< "30"不是别人.这样的查询是否可行,还是我必须先使用搜索获取整个文档,然后使用PHP选择此类访问??

I want to print only the visits with "payment" < "30" not others. Is such query possible, or do i have to get entire document first using search and then use PHP to select such visits??

推荐答案

在示例文档中,"payment"值作为字符串给出,可能无法与$ lt命令一起使用.对于此响应,我已将它们转换为整数.

In the example document, the "payment" values are given as strings which may not work as intended with the $lt command. For this response, I have converted them to integers.

MongoDB无法进行通配符查询,因此对于给定的文档结构,必须知道子文档的键(0、1,2等).例如,以下查询将起作用:

Wildcard queries are not possible with MongoDB, so with the given document structure, the key (0,1,2, etcetera) of the sub-document must be known. For instance, the following query will work:

> db.test.find({"visits.2.payment":{$lt:35}})

但是

> db.test.find({"visits.payment":{$lt:35}})

在这种情况下将不起作用,并且

Will not work in this case, and

> db.test.find({"visits.*.payment":{$lt:35}})

也不会返回任何结果.

will also not return any results.

为了能够查询嵌入的访问"文档,必须更改文档结构并将访问"设置成数组或嵌入文档,如下所示:

In order to be able to query the embedded "visits" documents, you must change your document structure and make "visits" into an array or embedded documents, like so:

> db.test2.find().pretty() { "_id" : ObjectId("4f16199d3563af4cb141c547"), "dob" : "10-01-1998", "name" : "Sam", "visits" : [ { "service_date" : "12-5-2011", "payment" : 30, "chk_number" : "86786464" }, { "service_date" : "12-15-2011", "payment" : 35, "chk_number" : "45643461234" }, { "service_date" : "12-25-2011", "payment" : 20, "chk_number" : "4569821234" } ] }

现在您可以在访问"中查询所有嵌入式文档:

Now you can query all of the embedded documents in "visits":

> db.test2.find({"visits.payment":{$lt:35}})

有关更多信息,请参阅有关点表示法的Mongo文档:

For more information, please refer to the Mongo documentation on dot notation:

www.mongodb/display/DOCS/Dot +符号+%28Reaching + into + Objects%29

现在进入问题的第二部分:不可能仅返回嵌入式文档的条件子集.

Now on to the second part of your question: it is not possible to return only a conditional sub-set of embedded documents.

使用任何一种文档格式,都不可能返回仅包含与查询匹配的子文档的文档.如果子文档之一与查询匹配,则整个文档与查询匹配,它将被返回.

With either document format, it is not possible to return a document containing ONLY the sub-documents that match the query. If one of the sub-documents matches the query , then the entire document matches the query, and it will be returned.

根据Mongo文档检索字段的子集"

As per the Mongo Document "Retrieving a subset of fields"

www.mongodb/display/DOCS/Retrieving+a +字段的子集+

我们可以像这样返回嵌入式文档的一部分:

We can return parts of embedded documents like so:

> db.test2.find({"visits.payment":{$lt:35}},{"visits.service_date":1}).pretty() { "_id" : ObjectId("4f16199d3563af4cb141c547"), "visits" : [ { "service_date" : "12-5-2011" }, { "service_date" : "12-15-2011" }, { "service_date" : "12-25-2011" } ] }

但是我们不能有条件地检索某些子文档.我们可以获取的最接近的是$ slice运算符,但这不是有条件的,您必须首先知道数组中每个子文档的位置:

But we cannot have conditional retrieval of some sub documents. The closest that we can get is the $slice operator, but this is not conditional, and you will have to first know the location of each sub-document in the array:

www.mongodb/display/DOCS /Retrieving + a + Subset + of + Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements

为了使应用程序仅显示与查询匹配的嵌入式文档,必须以编程方式完成.

In order for the application to display only the embedded documents that match the query, it will have to be done programmatically.

更多推荐

Mongodb嵌套数组搜索

本文发布于:2023-11-22 15:27:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数组   Mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!