Mongodb查询以相同的键合并文档

时间:2019-02-13 10:03:26

标签: mongodb

我在mongodb中有以下json数据。我想用相同的键合并所有这些文档。

{ 
"_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"
    }
 }

任何解决方案都很棒!

1 个答案:

答案 0 :(得分:1)

您可以像这样通过汇总获得结果

    db.collection.aggregate([
      {
        $group: {
          _id: null,
          AllBoiler: {
            $push: "$Boiler"
          }
        }
      },
      {
        $project: {
          _id: 0,
          Boiler: "$AllBoiler"
        }
      }
    ])

我还创建了mongo游乐场进行测试:https://mongoplayground.net/p/Ox0V-wlp3Z3