MongoDb,查询最后一个,分组依据 [英] MongoDb, query the last, and group by

查看:16
本文介绍了MongoDb,查询最后一个,分组依据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我在处理 MongoDb 时遇到了麻烦.

I m actually having trouble when dealing with MongoDb.

我需要:

  • 获取所有最后的条目
  • 关于一个领域
  • like : SELECT MAX(id), foreignKey FROM t_table GROUP BY foreignKey

我知道我们可以在 mongodb 中使用 $last,但我完全不知道如何进行.

I know that we can use $last with mongodb, but I dont know at all how to proceed.

我试过了:

db.collection.aggregate(
   [
     {
       $group:
         {
           _id: "$zone._id",
           lastRegistered: { $last: "$_id" }
         }
     }
   ]
)

但它似乎没有给我我需要的

But it does'nt seems to give me what I need

使用示例编辑

根据这三个:

  { _id: 55d5a01f9f58d2cc0eb79f5d,
    controlDate: Thu Aug 20 2015 11:38:40 GMT+0200 (Paris, Madrid (heure d’été)),
    zone:
     { _id: 55cb5bb42d191d2022c5c266,
       zoneName: 'Syphon 1',
     },
    actif: true 
    },



  { _id: 55d59f129f58d2cc0eb79f5c,
    controlDate: Fri Aug 21 2015 07:34:12 GMT+0200 (Paris, Madrid (heure d’été)),
    zone:
     { _id: 55cb5bb42d191d2022c5c266,
       zoneName: 'Syphon 1',
     },
    actif: true 
    }



  { _id: 55d5a01f9f58d2cc0eb79f5e,
        controlDate: Fri Aug 20 2015 08:38:40 GMT+0200 (Paris, Madrid (heure d’été)),
        zone:
         { _id: 55cb5bb42d191d2022c5c278,
           zoneName: 'Other zone',
         },
        actif: true 
        },

我需要:

  { _id: 55d59f129f58d2cc0eb79f5c,
    controlDate: Fri Aug 21 2015 07:34:12 GMT+0200 (Paris, Madrid (heure d’été)),
    zone:
     { _id: 55cb5bb42d191d2022c5c266,
       zoneName: 'Syphon 1',
     },
    actif: true 
    }



  { _id: 55d5a01f9f58d2cc0eb79f5e,
        controlDate: Fri Aug 20 2015 08:38:40 GMT+0200 (Paris, Madrid (heure d’été)),
        zone:
         { _id: 55cb5bb42d191d2022c5c278,
           zoneName: 'Other zone',
         },
        actif: true 
        },

这意味着:我需要获取按 Zone _id 分组的此集合的最后一个(按 ID 或日期,没关系).(2 行,因为我有一个区域的 2 个数据集(我只需要最后一个),而我需要另一个区域的最后一个(所以只有 1 行......))

Which means : I need to get the last (by id or date, never mind) of this collection grouped by the Zone _id. (2 lines, because I have 2 dataset for ONE zone (I need only the last) and I need the last of the other zone (only 1 line so..))

你明白我的意思吗?

推荐答案

您始终可以使用 $$ROOT 返回分组边界上的整个文档:

You can always use $$ROOT to return the whole document on the grouping boundary:

db.collection.aggregate([
    { "$group": { 
        "_id": "$zone._id",
        "doc": { "$last": "$$ROOT" }
    }}
 ])

或者使用直接排序控制而不是自然顺序:

Or with direct sort control rather than the natural order:

db.collection.aggregate([
    { "$sort": { "_id": 1 } },
    { "$group": { 
        "_id": "$zone._id",
        "doc": { "$last": "$$ROOT" }
    }}
  ])

但请注意,$group 管道不保证保留的文档顺序,因此如果您需要,则再次$sort:

But note that the $group pipeline does not guarantee a retained document order, so if you need that then you $sort again:

db.collection.aggregate([
    { "$sort": { "_id": 1 } },
    { "$group": { 
        "_id": "$zone._id",
        "doc": { "$last": "$$ROOT" }
    }},
    { "$sort": { "doc._id": 1 } }
 ])

在所有情况下,对 $$ROOT 的替代只是声明 $last 明确表示您希望从文档中获取的每个字段.相比之下,$max 运算符仅适用于指定的字段,因此当您需要像 $last 那样来自分组边界的文档时,这通常对您没有用.

In all cases the alterate to $$ROOT is simply to declare $last explicitly for each field you want from the document. By contrast the $max operator applies only to the specified field, so this is generally not of use to you when you want documents from the grouping boundary as $last does.

考虑那里的最后一个"示例并从您的示例中删除适当的数据,然后我得到:

Considering the "last" example there and also removing the appropriate data from your examples then I get:

{
    "_id" : "55cb5bb42d191d2022c5c266",
    "doc" : {
            "_id" : "55d5a01f9f58d2cc0eb79f5d",
            "zone" : {
                    "_id" : "55cb5bb42d191d2022c5c266",
                    "zoneName" : "Syphon 1"
            },
            "actif" : true
    }
},
{
    "_id" : "55cb5bb42d191d2022c5c278",
    "doc" : {
            "_id" : "55d5a01f9f58d2cc0eb79f5e",
            "zone" : {
                    "_id" : "55cb5bb42d191d2022c5c278",
                    "zoneName" : "Other zone"
            },
            "actif" : true
    }
}

改变的输入是这样的:

{
    "_id" : "55d5a01f9f58d2cc0eb79f5d",
    "zone" : {
            "_id" : "55cb5bb42d191d2022c5c266",
            "zoneName" : "Syphon 1"
    },
    "actif" : true
},
{
    "_id" : "55d59f129f58d2cc0eb79f5c",
    "zone" : {
            "_id" : "55cb5bb42d191d2022c5c266",
            "zoneName" : "Syphon 1"
    },
    "actif" : true
},
{
    "_id" : "55d5a01f9f58d2cc0eb79f5e",
    "zone" : {
            "_id" : "55cb5bb42d191d2022c5c278",
            "zoneName" : "Other zone"
    },
    "actif" : true
}

不用担心正确的 ObjectId 值,因为十六进制字符串值是词法的,就像 ObjectId 的内部排序一样.

Not bothering with correct ObjectId values in case as the hex string values are lexical just as the internal sort of an ObjectId would be anyway.

哪些是集合中每个提供的 zone._id 值的最后"文档,按给定的原始文档 _id 值排序.

Which are the "last" documents for each provided zone._id value from the collection, ordered by the original document _id value given.

这篇关于MongoDb,查询最后一个,分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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