[英]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" ] }
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.