Mongodb 在子数组中查找 [英] Mongodb find inside sub array

查看:22
本文介绍了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.

document" 包含所有的数组元素,甚至那些不匹配的元素,但条件表示document"确实匹配,所以它被返回.如果您只想要匹配的 "elements" 然后使用 .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 您可能会在一个文档中获得多次点击,因此考虑到 位置$ 运算符只会返回第一个匹配.

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 在子数组中查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆