我该如何使用MongoDB C#驱动程序执行$lookup操作?我在他们的驱动程序文档中找不到它:
https://docs.mongodb.org/getting-started/csharp/query/
但是如果我正确理解他们在JIRA中的这个问题,它应该在驱动程序的2.2版本中:
https://docs.mongodb.org/getting-started/csharp/query/
但是如果我正确理解他们在JIRA中的这个问题,它应该在驱动程序的2.2版本中:
var query = from p in collection.AsQueryable()
join o in otherCollection on p.Name equals o.Key into joined
select new { p.Name, AgeSum: joined.Sum(x => x.Age) };
这段内容是从MongoDB C#驱动程序文档(http://mongodb.github.io/mongo-csharp-driver/2.2/reference/driver/crud/linq/#lookup)中复制的。
otherCollection
是什么类型? - thepirat000MongoDB.Driver.Linq
命名空间来使它起作用。 - tigrouSystem.Linq
已经足够了。 - Beingnincollection.Aggregate()
.Lookup("foreignCollectionName", "localFieldName", "foreignFieldName", "result");
Collection.Aggregate().Lookup("foreignCollectionName", "localFieldName", "foreignFieldName","result").Project(Builders<BsonDocument>.Projection.Exclude("_id"))
.ToList()
那么你需要将其转换为JSON格式
String ConvertToJson= res[0].AsBsonDocument.ToJson();
String resultsConvertToJson = ConvertToJson.ToJson();
List<TModel> results= BsonSerializer.Deserialize<List<TMModel>>(resultsConvertToJson);
这对我起作用:
var collection2 = database.GetCollection<BsonDocument>("dbACESSO");
var match1 = new BsonDocument("$match", new BsonDocument("PartnerId", cliente));
var match2 = new BsonDocument("$match", new BsonDocument("CD_CLIENTE", codCond));
var lookup1 = new BsonDocument { { "$lookup", new BsonDocument { { "from", "GRUPO_UNIDADE" }, { "localField", "CD_GRUPO_UNIDADE" }, { "foreignField", "CD_GRUPO_UNIDADE" }, { "as", "GRUPO" } } } };
var pipeline = new[] { match1, match2, lookup1 };
var result = collection2.Aggregate<BsonDocument>(pipeline).ToList();
_fromTypeCollection.Aggregate<fromType>()
.Lookup<fromType,targetType,outputType>(targetTypeCollection,
fromType => fromType.localFeild,
targetType => targetType.foreignField,
outputType => outputType.result);
.find()
或.aggregate()
,只要它是有效的。因此,直接构建 BSON 文档作为管道即可。当然,前提是您有 MongoDB 3.2 服务器来支持$lookup
。这是决定性因素。 - Blakes Seven