检索与数组中的最大值匹配的子文档 [英] Retrieve sub-documents that match with maximum value in the array

查看:40
本文介绍了检索与数组中的最大值匹配的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongodb中具有以下数据结构

I have the following data structure in mongodb

[
    {
        "id" : "unique id 1",
        "timeStamp" : "timeStamp",
        "topicInfo" : [
            { 
                topic : "topic1", 
                offset : "offset number",
                time: 1464875267637
            },
            { 
                topic : "topic2", 
                offset : "offset number",
                time: 1464875269709
            },
            { 
                topic : "topic3", 
                offset : "offset number",
                time : 1464875270849
            }
       ]
   },
   {
       "id" : "unique id 2",
       "timeStamp" : "timeStamp",
       "topicInfo" : [
           { 
               topic : "15", 
               offset : "offset number",
               time : 1464875271884
           },
           { 
               topic : "topic2", 
               offset : "offset number",
               time : 1464875273887
           },
           { 
               topic : "topic3", 
               offset : "offset number",
               time : 1464875272848
           }
       ]
   }
 ]

现在,我想查找所有具有名为"topic2"的主题的条目,并且与"topicInfo"数组中的其他对象的时间相比,时间的值是最大的.我也想通过"timeStamp"对它们进行排序.从示例代码中,查询应返回第二个对象.我不能够写查询任何帮助将不胜感激.

Now I want to find all the entry That has topic called "topic2" and the value of time is maximum compare to other object's in the "topicInfo" array. I also want to sort them by "timeStamp". From the example code the query should return the second object. I am not able to write the query any help would be much appreciated.

推荐答案

做到这一点的最佳方法是在MongoDB 3.2或更高版本中.我们需要 $project 我们的文档,并使用 $filter 运算符返回与我们的匹配的"topicInfo"数组的子集健康)状况.从MongoDB3.2开始,我们可以使用 $max cond ition表达式的$project阶段中,对返回的值执行逻辑运算.

The optimal best way to do this is in MongoDB 3.2 or newer. We need to $project our documents and use the $filter operator to return a subset of the "topicInfo" array that matches our condition. And as of MongoDB3.2 , we can use the $max in the $project stage in the condition expression and perform a logical operation on the returned value.

计划的最后阶段是 $match 使用 $exists 元素查询运算符和 点符号 访问数组中的第一个元素.这也减少了通过有线网络发送的数据量,也减少了客户端上用于解码文档的时间和内存.

The final stage in the pipeline is the $match stage where you filter out those documents with empty "topicInfo" using the $exists element query operator and the dot notation to access the first element in the array. This also reduces both the amount of data sent over the wire and the time and memory used to decode documents on the client-side.

db.collection.aggregate([
    { "$project": { 
        "topicInfo": { 
            "$filter": { 
                "input": "$topicInfo", 
                "as": "t", 
                "cond": { 
                    "$and": [ 
                        { "$eq": [ "$$t.topic", "topic2"] }, 
                        { "$eq": [ "$$t.time", { "$max": "$topicInfo.time" } ] }
                    ] 
                } 
            } 
        } 
    }},
    { "$match": { "topicInfo.0": { "$exists": true } } }
])

这篇关于检索与数组中的最大值匹配的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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