将对象数组转换为mongodb中的字符串数组

Posted

技术标签:

【中文标题】将对象数组转换为mongodb中的字符串数组【英文标题】:Convert array of objects to array of strings in mongodb 【发布时间】:2022-01-14 19:15:37 【问题描述】:

我正在查看以下documentation。

以下将文档插入集合classes

db.classes.insertMany( [
    _id: 1, title: "Reading is ...", enrollmentlist: [ "giraffe2", "pandabear", "artie" ], days: ["M", "W", "F"] ,
    _id: 2, title: "But Writing ...", enrollmentlist: [ "giraffe1", "artie" ], days: ["T", "F"] 
] )

还有members 集合:

db.members.insertMany( [
    _id: 1, name: "artie", joined: new Date("2016-05-01"), status: "A" ,
    _id: 2, name: "giraffe", joined: new Date("2017-05-01"), status: "D" ,
    _id: 3, name: "giraffe1", joined: new Date("2017-10-01"), status: "A" ,
    _id: 4, name: "panda", joined: new Date("2018-10-11"), status: "A" ,
    _id: 5, name: "pandabear", joined: new Date("2018-12-01"), status: "A" ,
    _id: 6, name: "giraffe2", joined: new Date("2018-12-01"), status: "D" 
] )

他们使用以下聚合来连接数组字段enrollmentlist上的两个集合。

db.classes.aggregate( [
   
      $lookup:
         
            from: "members",
            localField: "enrollmentlist",
            foreignField: "name",
            as: "enrollee_info"
        
   
] )

返回以下内容:


   "_id" : 1,
   "title" : "Reading is ...",
   "enrollmentlist" : [ "giraffe2", "pandabear", "artie" ],
   "days" : [ "M", "W", "F" ],
   "enrollee_info" : [
       "_id" : 1, "name" : "artie", "joined" : ISODate("2016-05-01T00:00:00Z"), "status" : "A" ,
       "_id" : 5, "name" : "pandabear", "joined" : ISODate("2018-12-01T00:00:00Z"), "status" : "A" ,
       "_id" : 6, "name" : "giraffe2", "joined" : ISODate("2018-12-01T00:00:00Z"), "status" : "D" 
   ]


   "_id" : 2,
   "title" : "But Writing ...",
   "enrollmentlist" : [ "giraffe1", "artie" ],
   "days" : [ "T", "F" ],
   "enrollee_info" : [
       "_id" : 1, "name" : "artie", "joined" : ISODate("2016-05-01T00:00:00Z"), "status" : "A" ,
       "_id" : 3, "name" : "giraffe1", "joined" : ISODate("2017-10-01T00:00:00Z"), "status" : "A" 
   ]

如何将enrolle_info 缩减为包含所有names 的字符串数组?

这是我追求的结果:


   "_id" : 1,
   "title" : "Reading is ...",
   "enrollmentlist" : [ "giraffe2", "pandabear", "artie" ],
   "days" : [ "M", "W", "F" ],
   "enrollee_info" : [
      "artie",
      "pandabear"
      "giraffe2"
   ]


   "_id" : 2,
   "title" : "But Writing ...",
   "enrollmentlist" : [ "giraffe1", "artie" ],
   "days" : [ "T", "F" ],
   "enrollee_info" : [
      "artie",
      "giraffe1"
   ]

我还通过在$lookup 操作中引入pipeline 字段来研究使用多个连接。我可以使用$project 来获取仅使用"name": "example" 的数组,但我不确定如何删除"name"。我尝试过使用"$unwind": "$enrollee_info.name",但这并没有给我想要的东西。我是否需要在我进行连接之后向我的聚合管道引入另一个阶段?

【问题讨论】:

您可以在$lookup 之后引入$addFields 阶段 - 并使用$map 运算符按您的预期转换"enrollee_info" 【参考方案1】:

看来我把这个复杂化了。通过执行以下操作,我能够达到我想要的结果:

db.classes.aggregate( [
   
      $lookup:
         
            from: "members",
            localField: "enrollmentlist",
            foreignField: "name",
            as: "enrollee_info"
         
   ,
   
      $project:
          
              "_id": 1,
              "title": 1,
              "days": 1,
              "enrollee_names": "$enrollee_info.name"
          
    
] )

结果:

[
    
        "id": 1,
        "title": "Reading is ...",
        "days": [
            "M",
            "W",
            "F"
        ],
        "names": [
            "artie",
            "pandabear",
            "giraffe2"
        ]
    ,
    
        "id": 2,
        "title": "But Writing ...",
        "days": [
            "T",
            "F"
        ],
        "names": [
            "artie",
            "giraffe1"
        ]
    
]

【讨论】:

以上是关于将对象数组转换为mongodb中的字符串数组的主要内容,如果未能解决你的问题,请参考以下文章

将嵌套的 mongoDB 文档转换为平面 pandas DataFrame(对象数组中的对象数组)

将对象数组转换为虚拟 mongodb 集合

根据键中的子字符串将对象转换为对象数组

有没有办法在 MongoDB 聚合期间将字符串(格式:“1,5,20,22”)转换为整数数组( [1,5, 20, 22] )?

如何将带有参数数组的字符串 xml 转换为 .NET Core 中的对象

MongoDB如何将数组中的字段类型从字符串更改为数组并保持原始值