在MongoDB中的对象数组内查找对象ID

时间:2020-10-01 13:38:09

标签: mongodb aggregation-framework pymongo

这是我使用PyMongo客户端从MongoDB查询的Python代码:

def get(self):
    res  = json.loads(dumps( 
        self.devices_col.aggregate([
            {"$lookup":  {
                "from": "participants",
                "localField": "_id.docgroupid",
                "foreignField": "va_info.device_id",
                "as": "participants"
                }
            },
            {
                "$unwind": "$participants"
            }
        ])
     ))
    return res

示例文档:

参与者文档

{
  "_id" : ObjectId("5f7230502930714468ed892c"),
  "hash" : "83a84e8bf170114cffcc3b1e178d6468",
  "name" : "BOMW0000029529",
  "persona_id" : "i123",
  "command" : "start",
  "va_info" : [
    {
      "device_id" : "5f722a742930714468ed8929",
      "automation_config" : "",
      "status" : "false",
      "remote_path" : "/datadrive/gatewayfolder",
      "version" : "1.3.0.9",
      "latest_va_version" : "1.3.1.2",
      "version_updated_on" : "",
      "latest_va_build_number" : "20200525",
      "last_connected_on" : "02/08/2020 11:25:55",
      "last_seen_on" : "02/08/2020 11:25:55",
      "last_activity_processed_on" : "02/07/2020 11:25:55"
    }
  ],
  "inclusions" : [
    "myfinancewnscom",
    "OUTLOOK",
    "jp2launcher",
    "EXCEL"
  ],
  "created_by" : "",
  "created_on" : "",
  "modified_by" : "",
  "modified_on" : ""
}

设备文档

{
"_id" : ObjectId("5f722a742930714468ed8929"),
"name" : "",
"unique_id" : "u168381",
"os" : {
  "version" : "6.2.9200.0",
  "name" : "Microsoft Windows 10 Home",
  "locale" : {
    "geo_location" : null,
    "time_zone" : "IST",
    "day_light_saving_support" : false
  },
  "culture" : {
    "name" : "en-US",
    "LCID" : "1032",
    "language" : "English (United States)"
  },
  "browser" : [
    {
      "name" : "IE",
      "value" : "9.11.17763.0"
    },
    {
      "name" : "Chrome",
      "value" : "84.0.4147.105"
    },
    {
      "name" : "Firefox",
      "value" : "Not Found"
    }
  ]
},
"created_by" : "",
"created_on" : "",
"modified_by" : "",
"modified_on" : ISODate("2020-07-21T06:08:50.876Z")
}

在上面的代码中,我试图将两个集合(devicesparticipants)与device_id(其中是va_info数组的参与者)集合连接起来。

每个收藏集中只有两个文档。

但是我得到4个结果。其中两条记录似乎重复。

我要去哪里错了?

0 个答案:

没有答案