MongoDB lists - get every Nth item

前端 未结 6 2463
太阳男子
太阳男子 2021-02-13 05:36

I have a Mongodb schema that looks roughly like:

[
  {
    \"name\" : \"name1\",
    \"instances\" : [ 
      {
        \"value\" : 1,
        \"date\" : ISODate         


        
6条回答
  •  半阙折子戏
    2021-02-13 05:46

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

    Create a collection Names with the following schema

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

    and then Instances collection having the parent id as "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") }
    ]
    

    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"
      }}
    ])
    

    You can test it here

提交回复
热议问题