简体   繁体   English

Mongodb找到子数组

[英]Mongodb find inside sub array

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
        ]
    ]
}

This data could be spread across multiple documents. 这些数据可以分布在多个文档中。 In general, each document contains data in the info for 59 periods (seconds). 通常,每个文档包含59个周期(秒)的信息中的数据。

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? 有什么想法我会这样做吗?

Thank you 谢谢

EDIT: 编辑:

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. 你在正确的轨道上,但这里有一些注意事项,除了嵌套数组(尤其是匿名键)的部分并不是一个很好的存储方式,但只要你一直知道位置然后这应该是合理的。

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. 虽然您的当前值实际上不匹配(您的搜索值不在文档的范围内),但如果该值实际有效,则您的查询正确匹配此处的“文档” ,其中包含数组中的匹配元素。

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: 如果你只想要匹配的“元素”,那么使用.aggregate()代替:

    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
            ]
    ]
}

Or course if you only ever expected one element to match, then you could simply use projection with 或者当然,如果你只想要一个元素匹配,那么你可以简单地使用投影 .find() ** : .find() **

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

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. 但是使用像$gt这样的术语,您可能会在文档中获得多次点击,因此考虑到位置$运算符只会返回第一个匹配项,因此总体方法会更安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM