I am been trying to find an object in an array based on the key value, from the data in mongodb, bellow is the query from the database, but i only need the some specific object, how can achieve that ?
I am been trying to find an object in an array based on the key value, from the data in mongodb, bellow is the query from the database, but i only need the some specific object, how can achieve that ?
[
{
"_id": "627261a17acf7875b30d6e34",
"orderItem": [
[
{
"_id": "626fdb9fd0867e51d613530d",
"productName": "Handmade jewelry",
"productPrice": "25.99",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productimg4.jpg-1651497883882",
"sellerId": "62470b37f2052b6a6463f9b3",
"qty": 1
},
{
"_id": "626fdbf4d0867e51d6135389",
"productName": "Handmade bucket",
"productPrice": "23.99",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/topSellingImg.jpg-1651497970510",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823", "sellerId": "62470b37f2052b6a6463f9b3",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "62470b37f2052b6a6463f9b3",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"qty": 2
},
{
"_id": "626a5dabae8851064d4760af",
"productName": "hand bag",
"productPrice": "20",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b45",
"qty": 1
}
],
[
{
"_id": "626a5d5dae8851064d4760ac",
"productName": "handmade bag and show",
"productPrice": "5",
"proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "62470b37f2052b6a64637788",
"qty": 2
},
],
],
"__v": 0
}
]
Here is my code sample from node
router.get("/orders", async (req, res) => {
const { sellerID } = req.query;
try {
const order = await SellerOrder.find({
sellerId: sellerID ,
});
res.status(200).json(order);
} catch (error) {
res.status(500).json(error "error fetching data");
}
});
CodePudding user response:
For filter, you can try the nested $elemMatch
operator because your data has nested array,
Try aggregation operator to filter array in projection, you can add your required fields as well in projection,
$reduce
to iterate loop oforderItem
, set initial value as a blank array$filter
to iterate loop of first-level array to checksellerId
and filter array$concatArrays
to concat current array with filtered array
const order = await SellerOrder.find({
orderItem: {
$elemMatch: {
$elemMatch: {
sellerId: sellerID
}
}
}
},
{
orderItem: {
$reduce: {
input: "$orderItem",
initialValue: [],
in: {
$concatArrays: [
{
$filter: {
input: "$$this",
cond: {
$eq: ["$$this.sellerId", sellerID]
}
}
},
"$$value"
]
}
}
}
// add other projection fields here
})