Mongodb 是否从每个文档上的ObjectId数组中获取不同的相关数据?

Mongodb 是否从每个文档上的ObjectId数组中获取不同的相关数据?,mongodb,mongoose,Mongodb,Mongoose,因此,我收集了一些组织,如: [ { "name": "Organization 1", "locations": [ "5b0096f064386ef34a5118d0", // Location 1 "5b0096f064386ef34a5118d2" // Location 2 ] }, { "name": "Organization 2",

因此,我收集了一些组织,如:

[
    {
        "name": "Organization 1",
        "locations": [
            "5b0096f064386ef34a5118d0", // Location 1
            "5b0096f064386ef34a5118d2" // Location 2
        ]
    },
    {
        "name": "Organization 2",
        "locations": [
            "5b0096f064386ef34a5118d0" // Location 1
        ]
    }
    // ~1,000 organizations ...
]
[
    {
        // Location 1
        "_id": "5b0096f064386ef34a5118d0",
        "state": "AK",
        "city": "Adak"
    },
    {
        // Location 2
        "_id": "5b0096f064386ef34a5118d2",
        "state": "AK",
        "city": "Akhiok"
    },
    // ~36,000 locations ...
    {
        // Location 3
        "_id": "5b0096f064386ef34a51a63e",
        "state": "WY",
        "city": "Yoder"
    }
]
以及一组位置,如下所示:

[
    {
        "name": "Organization 1",
        "locations": [
            "5b0096f064386ef34a5118d0", // Location 1
            "5b0096f064386ef34a5118d2" // Location 2
        ]
    },
    {
        "name": "Organization 2",
        "locations": [
            "5b0096f064386ef34a5118d0" // Location 1
        ]
    }
    // ~1,000 organizations ...
]
[
    {
        // Location 1
        "_id": "5b0096f064386ef34a5118d0",
        "state": "AK",
        "city": "Adak"
    },
    {
        // Location 2
        "_id": "5b0096f064386ef34a5118d2",
        "state": "AK",
        "city": "Akhiok"
    },
    // ~36,000 locations ...
    {
        // Location 3
        "_id": "5b0096f064386ef34a51a63e",
        "state": "WY",
        "city": "Yoder"
    }
]
每个组织都有一个位置ID数组(引用位置集合)

我希望能够获得一个不同的“已使用”/引用位置的列表,以及填充的数据(如城市和州)

例如,在上面的场景中,我想返回:

[
    {
        // Location 1
        "_id": "5b0096f064386ef34a5118d0",
        "state": "AK",
        "city": "Adak"
    },
    {
        // Location 2
        "_id": "5b0096f064386ef34a5118d2",
        "state": "AK",
        "city": "Akhiok"
    }
]
…因为位置1和位置2是唯一实际引用的位置


关于如何实现这一点,您有什么想法吗?

不要费心在一个查询中尝试。要执行此操作,您将面临异步挑战:

    MongoOrganization.findById(org_id, function (error, org) {
      if (error) res.send(error);

      let locations;

      MongoLocations.find({'_id': { $in: org.locations } }, function (err, locs) {
        if (err) res.send(err);
        locations = locs;
      })

      org.locations = locations;

      res.json(org);
    })
取而代之的是获得orgs:

MongoOrganization.findById(org_id, function (error, org) {
   if (error) res.send(error);
   res.json(org);
})
然后再打第二个电话获取位置:

MongoLocations.find({'_id': { $in: org.locations } }, function (err, locations) {
   if (err) res.send(err);
   res.json(locations);
})

然后用对象数组复制字符串数组

如果它们是“实际引用的”,则只需
组织即可。distinct(“locations”)
将实际返回
ObjectId
值的“distinct list”。请记住,即使这个“列表”在“技术上”仍然是一个BSON对象,与其他所有对象具有相同的16MB约束。因此,您可以将其作为一个单独的查询,然后从“locations”集合中获取匹配的项。对于任何其他内容,您“可以”
$group
不同的条目和
$lookup
相关的详细信息,但这会带来一些开销。这真的取决于你想要的结果是什么。这个答案结合尼尔·伦的评论帮助我找到了一个解决方案。我用
OrganizationModel.distinct('locations')
检索了distinct Location ObjectId(
locationid
),然后用
LocationModel.find({u id:{$in:locationIds}})
来获取整个位置对象。