如何在 userid 的帮助下找到数组中的对象..?

how to find an object inside an array with the help of userid..?

  "_id": {
    "$oid": "5ddd17cb37c0b926986df00e"
  },
  "userId": "12345",
  "product": [
    {
      "productid": "5ddd11687ddacf31807e166d",
      "productname": "mango",
      "productprice": 100,
      "stock": 5
    }, {
      "productid": "5ddd11687dda123571807e114",
      "productname": "apple",
      "productprice": 100,
      "stock": 10
    }
  ],
  "total": 1700
}

我必须在产品名称和用户 ID 的帮助下找到 apple 的产品价格...任何人都可以帮助我吗...我尝试这样做但它返回未定义

const key = {userId : 12345};

    db.collection("carts").find(key,product).toArray(function(err,res){
        if(err) throw err;

        console.log(res.productprice);

    })



尝试这样的事情:

const key = {userId : 12345};

db.collection("carts").find(key, {products: {$elemMatch: {productname: "apple"}}}).toArray(function(err,res){
    if(err) throw err;
    console.log(res[0].products[0].productprice);
})
    const key = {userId : 12345};

    const producttemp = apple;
db.collection("carts").findOne(key,function(err,res){
    if(err) throw err;

    function productname(array,productname,value){
        for (var i = 0; i < array.length; i++) {
            if (array[i][productname] === value) {
                return array[i];
    }
}
    }
    const product = productname(res.product,"productname",producttemp);
    console.log(product.productprice);