[英]mongodb Query in c# using in operator
我正在嘗試使用其驅動程序在c#中轉換mongodb本機查詢,但是以某種方式無法產生正確的結果。
db.food.find
(
{
$and:[
{min_price:{$gte:1}}
,{max_price:{$lte:50}}
,{food:{$in:["sausage","burger"]}}
,{location:{$in:["new york","chicago"]}}
]
}
)
有人可以幫我用C#代碼嗎?
根據文檔 ,您可以嘗試Query.And
帶params的Query.And
方法 ,在其中傳遞單個Query
對象,如下所示:
var food = server.GetDatabase("food", credentials);
var col = db.GetCollection<RawBsonDocument>("bar");
foreach (var doc in col.Find(
Query.And(
Query.GTE("min_price", 1),
Query.LTE("max_price", 50),
Query.In("food", new [] { (BsonValue)"sausage", (BsonValue)"burger" }),
Query.In("location", new [] { (BsonValue)"new york", (BsonValue)"chicago" })
))
)
{
// handle found doc here
}
在此示例中,我使用了RawBsonDocument
類,但是如果您的數據很大,則可以使用LazyBsonDocument
。 從隱式經營string
來BsonValue
實現,並且可以在使用,然后.In
方法。
完整的文檔 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.