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

Little Brain

I have a MongoDB collection, Groups. Each group has an array of members, referenced by the member's unique 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. I want to export them to a csv file using mongoexport.

I can export the ids of the relevant projects and their member lists:

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.

Anton

Use aggregation with $unwind and then $out to create a new collection that looks like you need. Then export this new collection to CSV file.

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

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:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to aggregate documents flow into a single document with an array field? (mongodb)

$setUnion to merge array from multiple documents MongoDB

How to merge array field in the documents in mongoldb

$unwind multiple arrays present in the embedded documents and show each array as single document as output in mongoDB

MongoDB : How to sort documents by array field?

mongodb: flatten/ merge object with arrays to single array for all documents in collection

Mongodb Indexing array field in the documents

Merge multiple array to single array

Merge multiple array into single array

How to find documents in MongoDb matching a field and subdocument field that are in an array

How do I convert a field from multiple documents into an array in an object with MongoDB?

How to update "order" field in mongodb multiple documents collection accroding to request array?

using RxJs forkJoin to merge multiple api calls into single output array

How to findOneAndUpdate single field with multi nested array documents

Merge two array field in mongoDB

MongoDB: Delete documents from array in multiple documents

How to retrieve MongoDB documents via selection by array field

How to add a field to array of subdocuments in all the documents in mongodb?

MongoDB Aggregate - How to check if a specific field value exists in array of documents

How to combine all documents' field of array together in MongoDB?

MongoDB - how to merge dict field of documents?

How to select a single field for documents in a MongoDB collection?

MongoDB: How to take multiple fields within a document and output their values into an array (as a new field)?

finding documents in mongodb by array for one field?

mongodb: match documents without substring in array field

mongodb - concat array field from many documents to one array field

MongoDB - Update multiple subdocuments in array in multiple documents

Merge multiple array references/pointers in single array

MongoDB: How to update multiple documents with a single command?