【发布时间】:2019-02-03 10:44:05
【问题描述】:
我在 MongoDB 中有一个集合,其中包含我需要工作的集合名称的文档。我需要查询这个集合,从这个集合内的文档中获取所有集合名称,然后查询这些集合并根据 ParentId 引用加入它们。以下是存储其他集合名称的集合
db.AllInfoCollection.find()
{
"_id" : ObjectId("5b83b982a5e17c383c8424f3"),
"CollName" : "Collection1",
},
{
"_id" : ObjectId("5b83b9aaa5e17c383c8424f7"),
"CollName" : "Collection2",
},
{
"_id" : ObjectId("5b83b9afa5e17c383c8424f8"),
"CollName" : "Collection3",
},
{
"_id" : ObjectId("5b83b9b5a5e17c383c8424f9"),
"CollName" : "Collection4",
},
{
"_id" : ObjectId("5b83b9b9a5e17c383c8424fa"),
"CollName" : "Collection5",
},
{
"_id" : ObjectId("5b84f41bc5eb3f1f7c291f94"),
"CollName" : "Collection6",
}
上述所有集合(Collection1、Collection2、....Collection6)都是在运行时使用空文档创建的。它们通过 Id 和 ParentId 字段相互连接。 现在我需要查询这个 AllInfoCollection,获取集合名称并将它们连接起来并生成最终连接的 ($lookup) 输出。我能够查询并获取集合列表,但我不确定如何在 for 循环中添加查找投影。任何帮助,将不胜感激。
public void RetrieveDynamicCollection()
{
IMongoDatabase _db = client.GetDatabase("MyDb");
var collectionList = _db.GetCollection<AllInfoCollection>("AllInfoCollection").AsQueryable().Distinct().Select(x => x.CollectionName).ToList();
for(int i = 0; i < collectionList.Count; i++)
{
var collectionName = collectionList[i];
IMongoCollection<BsonDocument> collection = _db.GetCollection<BsonDocument>(collectionName);
var options = new AggregateOptions()
{
AllowDiskUse = false
};
//not able to proceed here
}
}
【问题讨论】:
标签: c# mongodb mongodb-query