Mongodb 合并集合中字段相同的对象
我正在尝试合并和更新4个字段(名称、面板、部分、小节)相同的对象 i、 e 数据合并1:名称='Field Handbook v1.1'和章节='customer'和小节='manuals'和面板='Revolution Series(R3 Series)' 数据合并2:名称='Field Handbook v1.1'和章节='customer'和小节='manuals'和面板='Eco-Safe Digester(E3系列)' 初始对象Mongodb 合并集合中字段相同的对象,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我正在尝试合并和更新4个字段(名称、面板、部分、小节)相同的对象 i、 e 数据合并1:名称='Field Handbook v1.1'和章节='customer'和小节='manuals'和面板='Revolution Series(R3 Series)' 数据合并2:名称='Field Handbook v1.1'和章节='customer'和小节='manuals'和面板='Eco-Safe Digester(E3系列)' 初始对象 { "_id" : ObjectId("5d35
{
"_id" : ObjectId("5d35e1fd02819f105326c84e"),
"deleted" : false,
"files" : [
{
"ext" : "pdf",
"file" : "Revolution Series Digester Field Handbook R3 Series v1.1 150 DPI.pdf"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Revolution Series (R3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c851"),
"deleted" : false,
"files" : [
{
"ext" : "link",
"file" : "http://docs.biohitech.com/r3/handbook"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Revolution Series (R3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c856"),
"deleted" : false,
"files" : [
{
"ext" : "pdf",
"file" : "Eco-Safe Digester Field Handbook E3 Series v1.1 150 DPI.pdf"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Eco-Safe Digester (E3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c857"),
"deleted" : false,
"files" : [
{
"ext" : "link",
"file" : "http://docs.biohitech.com/e3/handbook/"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Eco-Safe Digester (E3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c84e"),
"deleted" : false,
"files" : [
{
"ext" : "pdf",
"file" : "Revolution Series Digester Field Handbook R3 Series v1.1 150 DPI.pdf"
},
{
"ext" : "link",
"file" : "http://docs.biohitech.com/r3/handbook"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Revolution Series (R3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c856"),
"deleted" : false,
"files" : [
{
"ext" : "pdf",
"file" : "Eco-Safe Digester Field Handbook E3 Series v1.1 150 DPI.pdf"
},
{
"ext" : "link",
"file" : "http://docs.biohitech.com/e3/handbook/"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Eco-Safe Digester (E3 Series)"
}
预期结果
{
"_id" : ObjectId("5d35e1fd02819f105326c84e"),
"deleted" : false,
"files" : [
{
"ext" : "pdf",
"file" : "Revolution Series Digester Field Handbook R3 Series v1.1 150 DPI.pdf"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Revolution Series (R3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c851"),
"deleted" : false,
"files" : [
{
"ext" : "link",
"file" : "http://docs.biohitech.com/r3/handbook"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Revolution Series (R3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c856"),
"deleted" : false,
"files" : [
{
"ext" : "pdf",
"file" : "Eco-Safe Digester Field Handbook E3 Series v1.1 150 DPI.pdf"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Eco-Safe Digester (E3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c857"),
"deleted" : false,
"files" : [
{
"ext" : "link",
"file" : "http://docs.biohitech.com/e3/handbook/"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Eco-Safe Digester (E3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c84e"),
"deleted" : false,
"files" : [
{
"ext" : "pdf",
"file" : "Revolution Series Digester Field Handbook R3 Series v1.1 150 DPI.pdf"
},
{
"ext" : "link",
"file" : "http://docs.biohitech.com/r3/handbook"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Revolution Series (R3 Series)"
}
{
"_id" : ObjectId("5d35e1fd02819f105326c856"),
"deleted" : false,
"files" : [
{
"ext" : "pdf",
"file" : "Eco-Safe Digester Field Handbook E3 Series v1.1 150 DPI.pdf"
},
{
"ext" : "link",
"file" : "http://docs.biohitech.com/e3/handbook/"
}
],
"name" : "Field Handbook v1.1",
"section" : "customer",
"subsection" : "manuals",
"tags" : [
"customer",
"manuals"
],
"panel" : "Eco-Safe Digester (E3 Series)"
}
您可以尝试以下方法:
db.collection.aggregate([
{
$unwind: {
path: "$files",
preserveNullAndEmptyArrays: true
}
},
{
$group: {
_id: {
"name": "$name",
"section": "$section",
"subsection": "$subsection",
"panel": "$panel"
},
data: {
$first: "$$ROOT"
},
files: {
$push: "$files"
}
}
},
{
$addFields: {
"data.files": "$files"
}
},
{
$replaceRoot: {
newRoot: "$data"
}
},
/** writes to new collection named 'collection_new' will override collection if that name already exists */
{ $out: "collection_new" }
])
测试:
一旦您觉得
collection\u new
中的数据良好,您可能需要删除现有集合并将其重命名为旧/实际名称,同时检查索引。如果您可以升级到>4.2
,那么您可以使用它将文档附加到现有的集合中,这在使用上要干净得多。那么您想重新更新数据库吗?你的MongoDB版本是什么?@whoami我有一个几乎相同的文档集合,唯一的区别是文件数组。我想将文档合并到一个文档中,并将两个文件数组连接在一起,然后删除用于创建新文档的文档。@whoami db v4.0.11版可以使用4.011
对您的集合进行聚合并将聚合结果写入其他集合(集合\u新建)&删除现有名称,然后将收藏重新命名为旧名称/实际名称。