MongoDB列表-获取第N个项目 [英] MongoDB lists - get every Nth item

查看:65
本文介绍了MongoDB列表-获取第N个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Mongodb模式,大致类似于:

I have a Mongodb schema that looks roughly like:

[
  {
    "name" : "name1",
    "instances" : [ 
      {
        "value" : 1,
        "date" : ISODate("2015-03-04T00:00:00.000Z")            
      }, 
      {
        "value" : 2,
        "date" : ISODate("2015-04-01T00:00:00.000Z")
      }, 
      {
        "value" : 2.5,
        "date" : ISODate("2015-03-05T00:00:00.000Z")
      },
      ...
    ]
  },
  {
    "name" : "name2",
    "instances" : [ 
      ...
    ]
  }
]

每个元素的实例数量可能很大.

where the number of instances for each element can be quite big.

有时我只想获取数据样本,也就是说,每获取3个实例或获取10个实例……就可以得到图片.

I sometimes want to get only a sample of the data, that is, get every 3rd instance, or every 10th instance... you get the picture.

我可以通过获取所有实例并将其过滤到服务器代码中来实现此目标,但是我想知道是否有一种方法可以通过使用一些聚合查询来实现.

I can achieve this goal by getting all instances and filtering them in my server code, but I was wondering if there's a way to do it by using some aggregation query.

有什么想法吗?

已更新

假设数据结构像下面@SylvainLeroux所建议的那样平坦,即:

Assuming the data structure was flat as @SylvainLeroux suggested below, that is:

[
  {"name": "name1", "value": 1, "date": ISODate("2015-03-04T00:00:00.000Z")},
  {"name": "name2", "value": 5, "date": ISODate("2015-04-04T00:00:00.000Z")},
  {"name": "name1", "value": 2, "date": ISODate("2015-04-01T00:00:00.000Z")},
  {"name": "name1", "value": 2.5, "date": ISODate("2015-03-05T00:00:00.000Z")},
  ...
]

获取第N个项目(特定name)的任务会更容易吗?

will the task of getting every Nth item (of specific name) be easier?

推荐答案

您可能会喜欢使用 $lookup 聚合.可能是没有任何聚合技巧的最方便,最快的方法.

You might like this approach using the $lookup aggregation. And probably the most convenient and fastest way without any aggregation trick.

使用以下架构创建集合 Names

Create a collection Names with the following schema

[
  { "_id": 1, "name": "name1" },
  { "_id": 2, "name": "name2" }
]

然后是 Instances 集合,其父ID为"nameId"

[
  { "nameId": 1, "value" : 1, "date" : ISODate("2015-03-04T00:00:00.000Z") },
  { "nameId": 1, "value" : 2, "date" : ISODate("2015-04-01T00:00:00.000Z") },
  { "nameId": 1, "value" : 3, "date" : ISODate("2015-03-05T00:00:00.000Z") },
  { "nameId": 2, "value" : 7, "date" : ISODate("2015-03-04T00:00:00.000Z") }, 
  { "nameId": 2, "value" : 8, "date" : ISODate("2015-04-01T00:00:00.000Z") }, 
  { "nameId": 2, "value" : 4, "date" : ISODate("2015-03-05T00:00:00.000Z") }
]

现在使用 $lookup 聚合 3.6 语法,您可以使用 $sample $lookup pipeline 随机获取每个 Nth 元素.

Now with $lookup aggregation 3.6 syntax you can use $sample inside the $lookup pipeline to get the every Nth element randomly.

db.Names.aggregate([
  { "$lookup": {
    "from": Instances.collection.name,
    "let": { "nameId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": ["$nameId", "$$nameId"] }}},
      { "$sample": { "size": N }}
    ],
    "as": "instances"
  }}
])

您可以>在此处

这篇关于MongoDB列表-获取第N个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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