Soner, 我认为你可以使用聚合(或复杂事物的MapReduce)。即:
async void Main()
{
var client = new MongoClient("mongodb://localhost");
var db = client.GetDatabase("TestSPLike");
var col = db.GetCollection<Rule>("rules");
await client.DropDatabaseAsync("TestSPLike"); // recreate if exists
await InsertSampleData(col); // save some sample data
var data = await col.Find(new BsonDocument()).ToListAsync();
//data.Dump("All - initial");
/*
db.rules.aggregate(
[
{ $match:
{
"Active":true
}
},
{ $project:
{
Name: 1,
Active: 1,
Weight:1,
Produce: { $multiply: [ "$Weight.Unit", "$Weight.Value" ] }
}
},
{ $match:
{
Produce: {"$gt":5}
}
}
]
)
*/
var aggregate = col.Aggregate()
.Match(new BsonDocument{ {"Active", true} })
.Project(new BsonDocument {
{"Name", 1},
{"Active", 1},
{"Weight",1},
{"Produce",
new BsonDocument{
{ "$multiply", new BsonArray{"$Weight.Unit", "$Weight.Value"} }
}}
})
.Match(new BsonDocument {
{ "Produce",
new BsonDocument{ {"$gt",5} }
}
})
.Project(new BsonDocument {
{"Name", 1},
{"Active", 1},
{"Weight",1}
});
var result = await aggregate.ToListAsync();
//result.Dump();
}
private async Task InsertSampleData(IMongoCollection<Rule> col)
{
var data = new List<Rule>() {
new Rule { Name="Rule1", Active = true, Weight = new Weight { Unit=1, Value=10} },
new Rule { Name="Rule2", Active = false, Weight = new Weight { Unit=2, Value=3} },
new Rule { Name="Rule3", Active = true, Weight = new Weight { Unit=1, Value=4} },
new Rule { Name="Rule4", Active = true, Weight = new Weight { Unit=2, Value=2} },
new Rule { Name="Rule5", Active = false, Weight = new Weight { Unit=1, Value=5} },
new Rule { Name="Rule6", Active = true, Weight = new Weight { Unit=2, Value=4} },
};
await col.InsertManyAsync(data,new InsertManyOptions{ IsOrdered=true});
}
public class Weight
{
public int Unit { get; set; }
public int Value { get; set; }
}
public class Rule
{
public ObjectId _id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public Weight Weight { get; set; }
}
这种语法不适合MongoDB。如果您想在字段上计算结果,请查看[汇总框架](http://docs.mongodb.org/manual/core/aggregation-introduction/)。与一般的“查找”查询表单相比,它可以处理更多与SQL相关的“字段操作”。 –
这个很好,谢谢。我坚持重用的方法是MongoDB不支持的复杂函数,上面只是一个基本的方法。由于查询是从UI构建的,因此它看起来像Linq表达式查询和系统许可可以动态地完成这些复杂的事情。使用聚合框架需要转换器来转换它们。如果你知道这样的事情,请给你的建议 – cyworld8x