首页 文章

对填充的子文档进行Mongoose限制和排序

提问于
浏览
1

我正在尝试排序和限制嵌套的子文档,在查找并尝试不同的解决方案后,我无法理解它 .

System

const systemSchema = mongoose.Schema({
  data: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Data'
  }],
  ...other
})

Data

const dataSchema = mongoose.Schema({
  measurements: [measurementSchema]
  ...other
})

Measurement

const measurementSchema = mongoose.Schema({
  date: {
    type: Date,
    required: true
  },
  ...other
}, { _id : true })

如上所述; 1系统 - >数据阵列 - >测量数组

我想按日期和限制订购测量 .

What I've tried

我尝试的第一件事是使用填充选项,如下所示:

return System
    .find({ _id: { $in: systemIds } })
    .populate({
      path: 'data',
      populate: {
        path: 'measurements',
        options: {
          sort: { date: 1 },      // <-- doesn't work
          limit: 5                // <-- does work
        }
      }
    })

像这样尝试它确实会限制测量数组,但不会对其进行排序 .

在那之后我尝试使用聚合并且有很多尝试代码而且没有一个工作,所以我把它们排除在帖子之外 .

使用mongoDB 3.4.7

Sample data

{
    "systems": [
        {
            "data": [
                {
                    "_id": "5c0d87f77f93b06476be9cfd",
                    "measurements": [
                        {
                            "_id": "5c0d894178fc6c64cd36ffeb",
                            "value": "17",
                            "date": "2018-12-09T21:29:37.662Z"
                        }
                        {
                            "_id": "5c0d8952e418d464d6268bfc",
                            "value": "23",
                            "date": "2018-12-09T21:29:54.354Z"
                        },
                        {
                            "_id": "5c0d894b78fc6c64cd36fffb",
                            "value": "34",
                            "date": "2018-12-09T21:29:47.770Z"
                        },
                        {
                            "_id": "5c0d89503de6d564d2d32385",
                            "value": "19",
                            "date": "2018-12-09T21:29:52.293Z"
                        },
                        {
                            "_id": "5c0d894678fc6c64cd36fff3",
                            "value": "16",
                            "date": "2018-12-09T21:29:42.766Z"
                        },

                    ]
                }          
            ],
            "_id": "5c0d87f77f93b06476be9d01"
        }
    ]
}

2 回答

  • 0

    如果您可以更新到mongodb 3.6 ,那么您可以使用以下$lookup聚合

    db.collection.aggregate([
      { "$match": { "_id": { "$in": systemIds }},
      { "$lookup": {
        "from": Data.collection.name,
        "let": { "data": "$data" },
        "pipeline": [
          { "$match": { "$expr": { "$in": [ "$_id", "$$data" ] } } },
          { "$lookup": {
            "from": Measurement.collection.name,
            "let": { "measurements": "$measurements" },
            "pipeline": [
              { "$match": { "$expr": { "$in": [ "$_id", "$$measurements" ] } } },
              { "$sort": { "date": 1 }},
              { "$limit": 5 }
            ],
            "as": "measurements"
          }}
        ],
        "as": "data"
      }}
    ])
    
  • 0

    填充和排序是 mongoose 的一个问题:

    Issue 1 - 在填充中排序不起作用

    Issue 2 - 在子文档中路径的填充期间排序...

    您可能更好地使用 $lookup 进行聚合并通过 $project 排序为example can be seen here

相关问题