我需要填充一些索引数组

时间:2018-11-12 12:30:50

标签: mongodb mongoose mongodb-query aggregation-framework

我需要在mongodb中填充行的特定索引。

这是我的收藏集

{

     "startTime": "00:00",
     "endTime": "23:30",
     "services": [
                ObjectId("5b90e29729d36e0014d2f6f2"),
                ObjectId("5b8f96003b918f0014bb4b92"),
                ObjectId("5b8f6273621c0f02a9ae30b9"),
                ObjectId("5b90e33329d36e0014d2f6ff"),
                ObjectId("5b8e6fd47365a5001413062e"),
                ObjectId("5b8fb97b06d94a00143a11e7"),
                ObjectId("5ba88de21aabd703443a411e"),
                ObjectId("5b98e50ed6c3f80014d3c2cf"),
                ObjectId("5b8e853f7365a500141308c1"),
                ObjectId("5b9b7832d9f1070014352893"),
                ObjectId("5bb1d6bf35c2de001301c99c")
            ],
         "isActive": true,
        "isDeleted": false,
}

我只需要填充这些服务ID ObjectId(“ 5b90e29729d36e0014d2f6f2”),                 ObjectId(“ 5b8f96003b918f0014bb4b92”)。

1 个答案:

答案 0 :(得分:1)

您可以在mongodb 3.6

中使用$lookup聚合
var ids = [ObjectId("5b90e29729d36e0014d2f6f2"), ObjectId("5b8f96003b918f0014bb4b92")]

db.collection.aggregate([
  { "$lookup": {
    "from": "services",
    "pipeline": [{ "$match": { "$expr": { "$in": ["$_id", ids ]}}}],
    "as": "services"
  }}
])