MongoDB lists - get every Nth item

前端 未结 6 2375
太阳男子
太阳男子 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:57

    No $unwind is needed here. You can use $push with $arrayElemAt to project the array value at requested index inside $group aggregation.

    Something like

    db.colname.aggregate(
    [
      {"$group":{
        "_id":null,
        "valuesatNthindex":{"$push":{"$arrayElemAt":["$instances",N]}
        }}
      },
      {"$project":{"valuesatNthindex":1}}
    ])
    

提交回复
热议问题