When I query this
const pendingOfThisShop = await ShopProfile.findOne({ shop: req.shop.id, "shopsAffiliate.status":"pending" },{ shopsAffiliate: 1, _id: 0 }
I get an object like this
{
"shopsAffiliate": [
{
"status": "approved",
"_id": "5db315a6de255a4444b0987b",
"affiliateId": "5db31263a362ed4ed84c7ad5"
},
{
"status": "pending",
"_id": "5db315c5de255a4444b0987d",
"affiliateId": "5db2b4713db4101e48836f0a"
}
]}
I get both status:"approved" and status:"pending". I only want to get those objects in shopsAffiliate array which have status of "pending". What should I do?
I get both status:"approved" and status:"pending"
your query is returning the expected result.
shopsAffiliate
is an array of nested documents. shopsAffiliate
itself is part of documents inside ShopProfile
collection. Your query checks for any document in ShopProfile
that meets following 2 conditions
shop: req.shop.id
"shopsAffiliate.status":"pending"
When it finds any document that matches both above mentioned conditions, it returns the whole document. It doesn't cares whether there are other documents in shopsAffiliate
array of the matching document that do not have pending
status, it just needs to find atleast 1 document inside shopsAffiliate
array that has pending
status, as soon as it finds one, it returns the whole document.
I only want to get those objects in shopsAffiliate array which have status of "pending". What should I do?
you can use aggregation operation to get desired result
const pendingOfThisShop = await ShopProfile.aggregate([
{
$match: {
shop: req.shop.id,
"shopsAffiliate.status": "pending"
}
},
{
$unwind: "$shopsAffiliate"
},
{
$match: { "shopsAffiliate.status": "pending" }
}
]);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加