簡體   English   中英

mongodb在C#中使用in運算符查詢

[英]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 從隱式經營stringBsonValue實現,並且可以在使用,然后.In方法。

完整的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM