首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >映射MongoDB聚合管道中的数据

映射MongoDB聚合管道中的数据
EN

Stack Overflow用户
提问于 2019-03-11 16:37:27
回答 1查看 32关注 0票数 0

这是从我的聚合管道返回的数据:

代码语言:javascript
运行
复制
/* 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聚合将此数据投影到如下所示的内容

代码语言:javascript
运行
复制
{
        "_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运算符来完成,但仍然无法完成。

有谁能给我一些建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-11 17:05:39

可以使用$replaceRoot聚合运算符来实现这一点

代码语言:javascript
运行
复制
db.produce.aggregate( [
   {
     $replaceRoot: { newRoot: "$data" }
   }
] )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55097958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档