简体   繁体   English

如何将多个文档中的数组字段合并到MongoDB中的单个输出数组中,并将mongoexport合并到csv中

[英]How to merge an array field in multiple documents into a single output array in MongoDB and mongoexport to csv

I have a MongoDB collection, Groups. 我有一个MongoDB集合,Groups。 Each group has an array of members, referenced by the member's unique id. 每个组都有一个成员数组,由成员的唯一ID引用。 A user can be a member of many groups. 用户可以是许多组的成员。

Group data looks like this: 组数据如下所示:

{ _id: 1, members: ['1', '3', '5'], status: 'active' }
{ _id: 2, members: ['4', '1', '10', '11'], status: 'inactive' }
{ _id: 3, members: ['1', '2', '9'], status: 'active' }

I'm trying to extract all members of active groups as a single array of member ids, without duplication. 我试图将活动组的所有成员提取为单个成员ID数组,而不重复。 I want to export them to a csv file using mongoexport. 我想使用mongoexport将它们导出到csv文件。

I can export the ids of the relevant projects and their member lists: 我可以导出相关项目的ID及其成员列表:

mongoexport -h localhost:3001 --db mydbname --collection groups --type=csv --fields _id,members --query '{"status": "active"}' --out mongotestoutput.txt

But I can't figure out how to merge the member lists into a single array. 但我无法弄清楚如何将成员列表合并到一个数组中。 I've been looking at Aggregation but I'm just getting lost among all the different options, I can't see which one would do what I need. 我一直在关注聚合,但我只是迷失在所有不同的选项中,我无法看到哪一个会做我需要的。 Very grateful for any help. 非常感谢任何帮助。

Use aggregation with $unwind and then $out to create a new collection that looks like you need. 使用$unwind聚合然后$out创建一个看起来像你需要的新集合。 Then export this new collection to CSV file. 然后将此新集合导出到CSV文件。

db.test1.insertMany([
  { _id: 1, members: ['1', '3', '5'], status: 'active' },
  { _id: 2, members: ['4', '1', '10', '11'], status: 'inactive' },
  { _id: 3, members: ['9'], status: 'active' }
])

{_id:0} here and below is used to suppress _id field {_id:0}此处和下面用于抑制_id字段

db.test1.aggregate([
  {$unwind: "$members"},
  {$project:{_id:0}},
  {$out:"test2"}
])


db.test2.find({},{_id:0})
{ "members" : "1", "status" : "active" }
{ "members" : "3", "status" : "active" }
{ "members" : "5", "status" : "active" }
{ "members" : "4", "status" : "inactive" }
{ "members" : "1", "status" : "inactive" }
{ "members" : "10", "status" : "inactive" }
{ "members" : "11", "status" : "inactive" }
{ "members" : "9", "status" : "active" }

Or if you need to get members by status in the array - add another $group , $addToSet stage: 或者,如果您需要在数组中按状态获取成员 - 添加另一个$group$addToSet阶段:

db.test1.aggregate([
  {$unwind: "$members"},
  {$project:{_id:0}},
  { "$group": { "_id": "$status", members:{$addToSet:"$members"} } },
  {$out:"test3"}
])

db.test3.find()
{ "_id" : "inactive", "members" : [ "4", "1", "10", "11" ] }
{ "_id" : "active", "members" : [ "1", "3", "5", "9" ] }

See MongoPlayground MongoPlayground

This question shows two ways to get the result I'm looking for. 这个问题显示了两种获得我正在寻找的结果的方法。 The accepted answer uses push, reduce and setUnion. 接受的答案使用push,reduce和setUnion。 The newer answer is simpler, using unwind and addToSet. 使用unwind和addToSet,更新的答案更简单。 Both work for me but I'm going with the simpler version: 两者都适合我,但我会使用更简单的版本:

db.collection.aggregate([
  { $match: { "status": "active" },
  { $unwind: "$members"},
  { $group:{
     _id: 0,
     selectedMembers: { $addToSet: '$members' }      
  }}
])

I'm extracting the array I want from the JSON object returned by this expression. 我正在从这个表达式返回的JSON对象中提取我想要的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mongoexport csv输出最后一个数组值 - mongoexport csv output last array values 如何将文档流聚合成一个带有数组字段的文档? (mongodb) - How to aggregate documents flow into a single document with an array field? (mongodb) $unwind 嵌入文档中存在多个 arrays 并将每个数组显示为单个文档,如 mongoDB 中的 output - $unwind multiple arrays present in the embedded documents and show each array as single document as output in mongoDB MongoDB:如何按数组字段对文档进行排序? - MongoDB : How to sort documents by array field? 文档中的Mongodb Indexing array字段 - Mongodb Indexing array field in the documents 如何将对象数组中的属性从mongoexport导出为CSV? - How do I mongoexport attributes from an array of objects to CSV? 如何在MongoDb中查找与数组中的字段和子文档字段匹配的文档 - How to find documents in MongoDb matching a field and subdocument field that are in an array mongoDB-如何在多个文档中查找和排序多个字段并将它们存储在单个数组中? - mongoDB - How to find and sort multiple fields in several documents and store them in a single array? 将多个数组合并为单个数组 - Merge multiple array to single array 如何在javascript中将多个数组合并到单个数组中 - How to merge merge multiple array to single array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM