猫鼬从查询中检索特定的数组索引(而不是整个数组)

时间:2018-08-08 17:02:04

标签: javascript mongodb mongoose

请考虑以下集合:

       > db.customers.find().pretty();

       {
            "_id" : ObjectId("5b675d8180db9c13ac5b5c16"),
            "name" : "John",
            "active": true,
            "purchasedItems" : [
                {
                    "item_id" : "5b639e2dbc55ef13a6e50d29",
                    "quantity" : 2,
                    "deliveryDate" : 1536095404603
                },
                {
                    "item_id" : "5b0f0dcc4d122607cff6ce87",
                    "quantity" : 33,
                    "deliveryDate" : 1536179151868
                },
                {
                    "item_id" : "5b3a74bd973d5014df9d7d4b",
                    "quantity" : 23,
                    "deliveryDate" : 1536265571029
                },
                {
                    "item_id" : "5b101be68244dd03fb64acdd",
                    "quantity" : 111,
                    "deliveryDate" : 1536351973191
                },
                {
                    "item_id" : "5b101be68244dd03fb64acdd",
                    "quantity" : 11,
                    "deliveryDate" : 1536092750426
                }
            ]
        },
        {
            "_id" : ObjectId("5b675d8180db9c13ac5b5c16"),
            "name" : "John",
            "active": true,
            "purchasedItems" : [
                {
                    "item_id" : "354639e2dbc55ef13a6e50d29",
                    "quantity" : 1,
                    "deliveryDate" : 1536095404603
                },
                {
                    "item_id" : "53340dcc4d122607cff6ce87",
                    "quantity" : 12,
                    "deliveryDate" : 1536179151868
                },
                {
                    "item_id" : "5b7654bd973d5014df9d7d4b",
                    "quantity" : 44,
                    "deliveryDate" : 1536265571029
                },
                {
                    "item_id" : "5b1014648244dd03fb64acdd",
                    "quantity" : 14,
                    "deliveryDate" : 1536351973191
                },
                {
                    "item_id" : "5b101be34344dd03fb64acdd",
                    "quantity" : 6,
                    "deliveryDate" : 1536092750426
                }
            ]
        }

如何在不返回完整的purchasedItems数组的情况下检索查询中的第n个purchasedItems元素?

我尝试不成功(考虑index是我要检索的数组索引):

CustomerModel.findOne(
      {
          _id: customer_id,
          active: true
      },
      { 
          $arrayElementAt: { purchasedItems: index }
      }
 )

错误:

Error: error: {
    "ok" : 0,
    "errmsg" : "Unsupported projection option: $arrayElementAt: { purchasedItems: 1.0 }",
    "code" : 2,
    "codeName" : "BadValue"
}

1 个答案:

答案 0 :(得分:1)

$ arrayElemAt 应该与 $ aggregation 管道一起使用。 要检索第n个PurchaseItems,请查看下面的汇总代码。

db.test.aggregate([{
        "$match": {
            _id: customer_id,
            active: true
        }
    },
    {
        "$project": {
            _id: 1,
            name: 1,
            purchasedItems: { $arrayElemAt: ["$purchasedItems", INDEX]}
        }
    }
])

这将返回具有以下结构的文档

{ 
    "_id" : ObjectId("5b6b2672d99b3f60dfd39e6b"), 
    "name" : "John", 
    "purchasedItems" : { "item_id" : "5b3a74bd973d5014df9d7d4b", "quantity" : 23, "deliveryDate" : 1536265571029 } 
}