Looking to extract suppliers and customers from the given data using mongodb aggregate function, projecting specific fields while removing the rest.
{
"_id" : ObjectId("577f9a25bea25d480d8f1895"),
"password" : "12345",
"mobile" : "9582223889",
"email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="741534165a171b19">[email protected]</a>",
"name" : "ashiush jindal",
"invoice" : [{
"name" : "Ashish Jindal",
"dname" : "jindalbe",
"email" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2e0f2d612c2022">[email protected]</a>",
"password" : "12345",
"role" : ["customer"]
}],
"inventory" : [{ }, {
"item" : "Levis Jeans",
"sku" : "12345",
"buy" : [{
"bp" : "jindalbe",
"qty" : 50,
"created" : "9/7/2016"
}]
}, {
"item" : "Levis Trouser",
"sku" : "123",
"selling_price" : 2000,
"buy" : [{
"bp" : "jindalbe",
"qty" : 90,
"created" : "9/7/2016",
"price_per_qty" : 1000
}, {
"bp" : "jindalbe",
"qty" : 60,
"created" : "9/7/2016",
"price_per_qty" : 1000
}, {
"bp" : "jindalbe",
"qty" : 60,...
Extracting supplier names from business partners list as shown below:
{
suppliers:["Ashish Kumar"]
}
Using aggregation query for this purpose:
The attempted solution is as follows:
[
{ $match: { "_id": ObjectId(req.headers["token"]) } },{
$project: {
suppliers: {
$filter: {
input: '$business_partner.role',
...