这是从我的聚合管道返回的数据:
/* 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")
}
}
聚合管道太大了,这就是我不想在这里添加它的原因。我希望省略这部分是可以的。现在,我想使用MongoDB聚合将此数据投影到如下所示的内容
{
"_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")
}
我确信一定有一种方法可以做到这一点,我正在尝试使用$map运算符来完成,但仍然无法完成。
有谁能给我一些建议吗?
发布于 2019-03-11 17:05:39
可以使用$replaceRoot聚合运算符来实现这一点
db.produce.aggregate( [
{
$replaceRoot: { newRoot: "$data" }
}
] )
https://stackoverflow.com/questions/55097958
复制相似问题