MongoDB在子数组中查找

编程入门 行业动态 更新时间:2024-10-16 00:26:10
本文介绍了MongoDB在子数组中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个这样设置的文档:

I have a document that's setup like this:

{ _id : ObjectId(), info : [ [ 1399583281000, 20.13 ], [ 1399583282000, 20.13 ], [ 1399583283000, 20.13 ], [ 1399583285000, 20.13 ], [ 1399583286000, 20.13 ] ] }

此数据可能分布在多个文档中.通常,每个文档在信息中包含59个周期(秒)的数据.

This data could be spread across multiple documents. In general, each document contains data in the info for 59 periods (seconds).

我想做的是获取所有时间戳大于特定时间的信息数据.

What I would like to do is get all of the info data where the timestamp is greater than a specific time.

有什么想法我会怎么做吗?

Any ideas how I would go about doing this?

谢谢

因此,我发现这似乎返回了所有文档:

So, I've found that this seems to return all of the documents:

db.infos.find({ info:{ $elemMatch:{ 0:{ $gt:1399583306000 } } } })

但是也许我需要在汇总查询中使用它?这样它将只返回所有值?

But maybe I need to use this in an aggregate query? so that it will return just all the values?

推荐答案

您在正确的轨道上,但是除了嵌套数组(尤其是带有匿名键的数组)不完全相同的部分以外,这里还有一些要注意的地方存储东西的好方法,但是只要您始终知道位置,那应该就可以了.

Your on the right track, but there are a few things to note here, aside from the part that nested arrays ( and especially with anonymous keys) are not exactly a great way to store things, but as long as you consistently know the position then that should be reasonably okay.

匹配的文档和匹配的数组元素" 之间有明显的区别.尽管您当前的值实际上不匹配(您的搜索值不在文档的范围内),但是如果该值实际有效,则您的查询正确匹配此处的"document" ,其中包含一个匹配元素在数组中.

There is a distinct difference between matching documents and matching "elements of an array". Though your current value would actually not match (your search value is not within the bounds of the document), if the value actually was valid your query correctly matches the "document" here, which contains a matching element in the array.

文档" 包含所有个数组元素,即使是不匹配的元素,但条件为文档" 确实匹配,因此将其返回.如果您只想要匹配的元素" ,请使用 .aggregate() 代替:

The "document" contains all of the array elements, even those that do not match, but the condition says the "document" does match, so it is returned. If you just want the matching "elements" then use .aggregate() instead:

db.infos.aggregate([ // Still match the document { "$match": { "info": { "$elemMatch": { "0": {"$gte": 1399583285000} } } }}, // unwind the array for the matched documents { "$unwind": "$info" }, // Match only the elements { "$match": { "info.0": { "$gte": 1399583285000 } } }, // Group back to the original form if you want { "$group": { "_id": "$_id", "info": { "$push": "$info" } }} ])

这将仅返回与条件匹配的元素:

And that returns just the elements that matched the condition:

{ "_id" : ObjectId("536c1145e99dc11e65ed07ce"), "info" : [ [ 1399583285000, 20.13 ], [ 1399583286000, 20.13 ] ] }

或者当然,如果您只希望匹配一个元素,则可以将投影与 .find()** :

Or course if you only ever expected one element to match, then you could simply use projection with .find()**:

db.infos.find( { "info":{ "$elemMatch":{ "0": { "$gt": 1399583285000 } } } }, { "info.$": 1 } )

但使用 $gt 之类的术语,您可能会在一个文档中获得多个匹配,因此考虑到 positional $ 运算符只会返回第一匹配.

But with a term like $gt you are likely to get multiple hits within a document so the aggregate approach is going to be safer considering that the positional $ operator is only going to return the first match.

更多推荐

MongoDB在子数组中查找

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

发布评论

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

>www.elefans.com

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