如何从 mongo db 中的对象数组中获取特定数组元素

How to fetch particular array elements from object's array in mongo db

我有一行,其中包含如下数据:

{
    "_id" : ObjectId("5bcef76b0c9a4c194cf6d0a7"),
    "userid" : ObjectId("5bc5ae4355418805b8caabb3"),
    "transactionid" : "ch_1DON67EzCa9AoDtY51kialzs",
    "adminid" : [
        "5b5af339bc69c511816e8a2f",
        "5b87948d97b752099c086708"
    ],
    "amount" : 3220,
    "ispaid" : true,
    "products" : [
        {
            "productid" : ObjectId("5bceba35003c87043997a1d4"),
            "quantity" : 2,
            "price" : 200,
            "type" : "product",
            "isCanceled" : false,
            "isDeliverd" : false,
            "createdby" : "schooladmin",
            "userid" : ObjectId("5b87948d97b752099c086708"),
            "isReadyToPickup" : false,
            "name" : "The Second Product",
            "description" : "                "
        },
        {
            "productid" : ObjectId("5bc5b2df55418805b8caabbd"),
            "quantity" : 2,
            "price" : 100,
            "type" : "product",
            "isCanceled" : false,
            "isDeliverd" : false,
            "createdby" : "superadmin",
            "userid" : ObjectId("5b5af339bc69c511816e8a2f")
        },
        {
            "productid" : ObjectId("5bc5bc5fe84c3d028aaa269c"),
            "quantity" : 2,
            "price" : 100,
            "type" : "product",
            "isCanceled" : false,
            "isDeliverd" : false,
            "createdby" : "superadmin",
            "userid" : ObjectId("5b5af339bc69c511816e8a2f")
        }
],
    "paymentUsing" : "card",
    "cardBrand" : "Visa",
    "country" : "US",
    "paymentDate" : "2018-10-23T10:26:51.856Z"
}

我想对 products 对象的所有元素执行搜索。如果其中任何一个匹配,则整个对象将存储在数组变量中。

假设,我尝试从 name 对象中搜索 The Second Product 字符串 产品 数组。然后它会给我所有元素。喜欢

[
    {
        "productid" : ObjectId("5bceba35003c87043997a1d4"),
        "quantity" : 2,
        "price" : 200,
        "type" : "product",
        "isCanceled" : false,
        "isDeliverd" : false,
        "createdby" : "schooladmin",
        "userid" : ObjectId("5b87948d97b752099c086708"),
        "isReadyToPickup" : false,
        "name" : "The Second Product",
        "description" : "                "
    } 
]

如果找到两个或更多元素,那么它将 return 相应地排列。

请给我建议解决方案。 先感谢您。 :)

像这样:

db.collection.find({products: {$elemMatch: {name:'The Second Product'}}})

使用aggregation pipeline我们可以得到想要的结果。

Approach 1:

如果我们没有嵌套数组,更简单的方法

$filter is used within $project 得到想要的结果

db.collection_name.aggregate([
  {
    $project: {
      products: {
        $filter: {
          input: "$products",
          as: "product",
          cond: {
            $eq: [
              "$$product.name",
              "The Second Product"
            ]
          }
        }
      }
    }
  }
])

Approach 2:

$unwind 放松 products array

$match 检索匹配的文档

$project 仅投影所需的输出元素

db.collection_name.aggregate([
 { $unwind: "$products" },
 { $match:{"products.name":"The Second Product"} }, 
 { $project:{products:1}} 
])

上述查询的输出(方法 1 或方法 2)

{
        "_id" : ObjectId("5bcef76b0c9a4c194cf6d0a7"),
        "products" : {
                "productid" : ObjectId("5bceba35003c87043997a1d4"),
                "quantity" : 2,
                "price" : 200,
                "type" : "product",
                "isCanceled" : false,
                "isDeliverd" : false,
                "createdby" : "schooladmin",
                "userid" : ObjectId("5b87948d97b752099c086708"),
                "isReadyToPickup" : false,
                "name" : "The Second Product",
                "description" : "                "
        }
}