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

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

问题描述

与MongoDb打交道时,我实际上遇到了麻烦.

I m actually having trouble when dealing with MongoDb.

我需要:

  • 获取所有最新条目
  • 关于字段
  • like:SELECT MAX(id),来自t_table GROUP BY ForeignKey的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天全站免登陆