从mongodb中的每个x文档中从集合中获取所有文档

时间:2020-05-12 04:13:21

标签: mongodb mongodb-query bigdata

我收集了一些数据日志,其中包含约8500万个文档,如下所示:

{
    id: 1,
    ....,
    timestamp: ISODate('2020-04-24T11:29:49.000Z')
},
{
    id: 1,
    ....,
    timestamp: ISODate('2020-04-24T11:25:49.000Z')
},
{
    id: 1,
    ....,
    timestamp: ISODate('2020-04-24T11:20:49.000Z')
},
{
    id: 1,
    ....,
    timestamp: ISODate('2020-04-24T11:16:49.000Z')
},
{
    id: 2,
    ....,
    timestamp: ISODate('2020-04-24T11:29:49.000Z')
},
{
    id: 3,
    ....,
    timestamp: ISODate('2020-04-24T11:25:49.000Z')
},
{
    id: 1,
    ....,
    timestamp: ISODate('2020-04-24T11:11:49.000Z')
},
...

如何获取每个xxx文档的所有 id = 1 数据,如下所示:

{
    id: 1,
    ....,
    timestamp: ISODate('2020-04-24T11:29:49.000Z')
},
{
    id: 1,
    ....,
    timestamp: ISODate('2020-04-24T11:20:49.000Z')
},
{
    id: 1,
    ....,
    timestamp: ISODate('2020-04-24T11:11:49.000Z')
},

我尝试使用 $ group ,但不完全匹配。 已使用$map and $range,但未成功。

我尝试过

db.logs.aggregate([
    {$match : {"id" : 1}},
    {$sort: {"timestamp":-1}},
    {$group : {'_id' : "$id",'docs' : {$push : "$$ROOT"}}},
    {$project : {
        "list" : {
          $map : {
            'input' : {$range : [0, {$size : '$docs'}, 3]}, #3 is x-step
            'as' : "index",
            'in' : {$arrayElemAt : ['$docs', '$$index']}
          }
    }}}
], {allowDiskUse:true})

但是我收到错误消息"errmsg" : "$push used too much memory and cannot spill to disk. Memory limit: 104857600 bytes"

感谢支持。

0 个答案:

没有答案