Reputation: 179
This is the data returned from my aggregation pipeline:
/* 1 */
{
"data" : {
"_id" : ObjectId("5c8266b8d8c9cd7a89babac6"),
"updatedBy" : ObjectId("5c2076d62781881e8764a47a"),
"updatedAt" : ISODate("2019-03-08T13:33:21.659Z"),
"createdBy" : ObjectId("5c2076d62781881e8764a47a"),
"createdAt" : ISODate("2019-03-08T12:57:28.683Z"),
"productName" : "Yellow powder",
"sku" : "563453534",
"upc" : "903453453245",
"__v" : 0,
"brand" : "",
"category" : "yellow category",
"group" : "",
"handle" : "yellow-grey",
"imageUrl" : "",
"inciRawText" : "",
"specialtyText" : ""
}
}
/* 2 */
{
"data" : {
"_id" : ObjectId("5c7692433dcd874313b9fddb"),
"sku" : "TESTSKU9",
"__v" : 0,
"brand" : "test brand",
"category" : "test category",
"createdAt" : ISODate("2019-02-27T13:36:03.027Z"),
"group" : "testgroup",
"handle" : "mewo9",
"imageUrl" : "",
"inciRawText" : "Inci Raw Text. Long memo field",
"productName" : "TEST9PROD",
"publishStatus" : "Unpublished",
"specialtyText" : "* denotes organic",
"upc" : "TESTUPC9",
"updatedAt" : ISODate("2019-02-27T13:36:03.027Z")
}
}
The aggregation pipeline is too big that's why I didn't wanted to add it here. I hope it's ok to omit that part. Now I want to project this data to something like this using MongoDB Aggregation
{
"_id" : ObjectId("5c8266b8d8c9cd7a89babac6"),
"updatedBy" : ObjectId("5c2076d62781881e8764a47a"),
"updatedAt" : ISODate("2019-03-08T13:33:21.659Z"),
"createdBy" : ObjectId("5c2076d62781881e8764a47a"),
"createdAt" : ISODate("2019-03-08T12:57:28.683Z"),
"productName" : "Yellow powder",
"sku" : "563453534",
"upc" : "903453453245",
"__v" : 0,
"brand" : "",
"category" : "yellow category",
"group" : "",
"handle" : "yellow-grey",
"imageUrl" : "",
"inciRawText" : "",
"specialtyText" : ""
}, {
"_id" : ObjectId("5c7692433dcd874313b9fddb"),
"sku" : "TESTSKU9",
"__v" : 0,
"brand" : "test brand",
"category" : "test category",
"createdAt" : ISODate("2019-02-27T13:36:03.027Z"),
"group" : "testgroup",
"handle" : "mewo9",
"imageUrl" : "",
"inciRawText" : "Inci Raw Text. Long memo field",
"productName" : "TEST9PROD",
"publishStatus" : "Unpublished",
"specialtyText" : "* denotes organic",
"upc" : "TESTUPC9",
"updatedAt" : ISODate("2019-02-27T13:36:03.027Z")
}
I'm sure there must be a method to do it, I'm trying to do it using $map operator, but still unable to get it done.
Can anyone please suggest me some solutions?
Upvotes: 0
Views: 100
Reputation: 1988
You can achieve that using $replaceRoot aggregation operator
db.produce.aggregate( [
{
$replaceRoot: { newRoot: "$data" }
}
] )
Upvotes: 3