用于合并具有相同密钥的文档的Mongodb查询
我在mongodb中有以下json数据。我想用相同的密钥合并所有这些文档用于合并具有相同密钥的文档的Mongodb查询,mongodb,Mongodb,我在mongodb中有以下json数据。我想用相同的密钥合并所有这些文档 { "_id" : ObjectId("5c628eef0f17e108ec305682"), "Boiler" : { "Warranty" : "Void", "Plant_Location" : "USA", "Asset_Status" : "Active", "Created_Date" : "12 Feb 2019", "Asset_Last_Service_
{
"_id" : ObjectId("5c628eef0f17e108ec305682"),
"Boiler" : {
"Warranty" : "Void",
"Plant_Location" : "USA",
"Asset_Status" : "Active",
"Created_Date" : "12 Feb 2019",
"Asset_Last_Service_Date" : "2011-07-23",
"Asset_SRN" : "ASDASDKLj131203-91",
"Asset_Name" : "Pressure Pump",
"Asset_Manufacturer" : "Thompson",
"Asset_Unit_of_Measurement" : "PSI",
"Asset_Inuse_from" : "2013-06-25",
"Asset_Manufactured_On" : "2015-04-23",
"Modified_Date" : "12 Feb 2019",
"Plant_Name" : "KCO",
"Asset_Location" : "China",
"Description" : "ALKSJDJKSA"
}
}
{
"_id" : ObjectId("5c629d110f17e11b180f9290"),
"Boiler" : {
"Asset_Name" : "Heater",
"Asset_Status" : "Active",
"Description" : "asdasd",
"Created_Date" : "12 Feb 2019",
"Asset_Inuse_from" : "2009-04-23",
"Asset_Manufacturer" : "JJSSJ",
"Asset_Last_Service_Date" : "2011-05-20",
"Plant_Location" : "HHJJJOO",
"Warranty" : "ASDASDASD",
"Asset_Unit_of_Measurement" : "QQWWEERRTT",
"Modified_Date" : "12 Feb 2019",
"Asset_Location" : "OIUIOU",
"Plant_Name" : "OIPOIPOI",
"Asset_SRN" : "08321940198",
"Asset_Manufactured_On" : "2010-05-23"
}
}
{
"_id" : ObjectId("5c62ab1a0f17e112e067aeff"),
"Boiler" : {
"Created_Date" : "12 Feb 2019",
"Asset_Status" : "Active",
"Modified_Date" : "12 Feb 2019",
"Plant_Location" : "POEWIRPOIQ",
"Description" : "LJKDSAJFWIOFW",
"Plant_Name" : "LKAJSALKJSD",
"Asset_SRN" : "LKJALKJF",
"Asset_Name" : "VCDCZXCXC",
"Asset_Manufacturer" : "POQEWIRPOWEIR",
"Warranty" : "ASDASDASD",
"Asset_Last_Service_Date" : "2002-12-09",
"Asset_Unit_of_Measurement" : ";AKS;DLKS;LDK",
"Asset_Inuse_from" : "2000-09-05",
"Asset_Location" : "KAJFLKSADJF",
"Asset_Manufactured_On" : "2001-11-18"
}
}
正如您在上面的json数据中看到的,存在重复的键,这些键具有相同的值和相同的键。我想将这些文档与所有数据合并在一起。是否可行
像这样
{ "Boiler" : {
"Warranty" : "Void",
"Plant_Location" : "Pune",
"Asset_Status" : "Active",
"Created_Date" : "12 Feb 2019",
"Asset_Last_Service_Date" : "2011-07-23",
"Asset_SRN" : "ASDASDKLj131203-91",
"Asset_Name" : "Pressure Pump",
"Asset_Manufacturer" : "Kirloskar",
"Asset_Unit_of_Measurement" : "PSI",
"Asset_Inuse_from" : "2013-06-25",
"Asset_Manufactured_On" : "2015-04-23",
"Modified_Date" : "12 Feb 2019",
"Plant_Name" : "Pune",
"Asset_Location" : "India",
"Description" : "ALKSJDJKSA"
},
{
"Asset_Name" : "Heater",
"Asset_Status" : "Active",
"Description" : "asdasd",
"Created_Date" : "12 Feb 2019",
"Asset_Inuse_from" : "2009-04-23",
"Asset_Manufacturer" : "JJSSJ",
"Asset_Last_Service_Date" : "2011-05-20",
"Plant_Location" : "HHJJJOO",
"Warranty" : "ASDASDASD",
"Asset_Unit_of_Measurement" : "QQWWEERRTT",
"Modified_Date" : "12 Feb 2019",
"Asset_Location" : "OIUIOU",
"Plant_Name" : "OIPOIPOI",
"Asset_SRN" : "08321940198",
"Asset_Manufactured_On" : "2010-05-23"
}
}
这方面的任何解决方案都将是伟大的 您可以通过这样的聚合来获得结果
db.collection.aggregate([
{
$group: {
_id: null,
AllBoiler: {
$push: "$Boiler"
}
}
},
{
$project: {
_id: 0,
Boiler: "$AllBoiler"
}
}
])
我还为测试创建了mongo游乐场:您是否尝试了db.collection.aggregate()@MebinJoe No。任何参考或示例都会有所帮助,谢谢!我不明白这里的复制钥匙是什么意思?您只想查询或重新排列数据库中的数据?@BùiĐứcKhánh Duplicate指每份文件都有副本,即“锅炉”。我要所有不同的锅炉数据在一个“锅炉”键下。该死!谢谢你的解决方案。现在我可以用这样的查询做任何事情!!!!