Mongoose 如何在mongodb中检索嵌套结构中的特定对象?
根据下面的结构,我想检索给定此shopName的第二个product对象。我只能考虑通过以下查询获取所有产品对象:Mongoose 如何在mongodb中检索嵌套结构中的特定对象?,mongoose,mongodb-query,Mongoose,Mongodb Query,根据下面的结构,我想检索给定此shopName的第二个product对象。我只能考虑通过以下查询获取所有产品对象:Shop.find({shopName:shopName})。选择('products')。我怎样才能修改它来达到我想要的,谢谢 "shopName": "jmart", "products": [{ "id": 1, "name": "Cle
Shop.find({shopName:shopName})。选择('products')
。我怎样才能修改它来达到我想要的,谢谢
"shopName": "jmart",
"products": [{
"id": 1,
"name": "Clean and Clear Deep Clean Cleanser 100g",
"slug": "8d1c895c-6911-4fc8-a34c-89c6948233d7",
"price": 4.5,
"discount_price": 0,
"category": "Health and Beauty",
"sale": false,
"subcategory": "personal care",
"color": "black",
"article": "Clean and Clear",
"quantity": 9,
"img": "https://firebasestorage.googleapis.com/v0/b/swifty-products.appspot.com/o/Jmart%2FBeauty%2FClean%20and%20Clear%20Deep%20Clean%20Cleanser%20100g.jpg?alt=media",
"vendor": {
"id": 1,
"name": "Clean and Clear"
},
"ratings": {
"star_ratings": 0,
"votes": 0
},
"isAvailable": true
}, {
"id": 2,
"name": "Colgate Total Pro Breath Health",
"slug": "67626dae-1537-40d8-837d-483e5759ada0",
"price": 4.5,
"discount_price": 0,
"category": "Health and Beauty",
"sale": false,
"subcategory": "personal care",
"color": "black",
"article": "Colgate",
"quantity": 9,
"img": "https://firebasestorage.googleapis.com/v0/b/swifty-products.appspot.com/o/Jmart%2FBeauty%2FColgate%20Total%20Pro%20Breath%20Health.jpg?alt=media",
"vendor": {
"id": 2,
"name": "Colgate"
},
"ratings": {
"star_ratings": 0,
"votes": 0
},
"isAvailable": true
},
]
可以使用运算符返回第n个数组元素
{$slice:[,]}
const products=wait Shop.find(
{shopName:shopName},
{products:{$slice:[1,1]}//跳过数组的第一个元素并返回下一个1元素
).选择(“产品”);
hi@Vishnu,你认为我如何根据slug更新第二个元素中的特定字段?以下操作无效Shop.find({shopName:shopName}).update({products:{$elemMatch:{slug:slug}}},{$set:{price:req.body.price}}