![](/img/trans.png)
[英]Query with filter builder on nested array using MongoDB C# driver
[英]Query with filter builder on nested array using MongoDB C# driver with a given array of string
考慮以下存儲為文檔的對象結構:
public class Foo
{
public string Id { get; set; }
public List<FooBar> Bars { get; set; }
// ...
}
public class FooBar
{
public string uid { get; set; }
// ...
}
我有一個字符串數組:
var ArrayOfUid = ["STK-00112","STK-00117","STK-00113","STK-00114"]
我想獲取FooBar.uid
中ArrayOfUid
所有產品
所以最后我得到了這樣的產品列表,沒有重復
在 MongoDb 端查詢:
db.collection.find({
"foobar.uid": {
$in: [
"STK-00113",
"STK-00117",
"STK-00113",
"STK-00114"
]
}
})
有沒有辦法用字符串數組來做到這一點? 目前我只能用一個給定的字符串來實現它:
var findFluent = collection.Find(Builders<Foo>.Filter.ElemMatch(
foo => foo.Bars,
foobar => foobar.uid == "STK-00113"));
這將返回具有
foobar.uid
= "STK-00113"
有沒有辦法遍歷字符串數組並返回所有對象的列表?
編輯
public List<Produit> Get() =>
_produits.Find(produit => true).ToList();
將返回此產品列表:
[
{
"_id": {
"$oid": "6152da109e0ced07f16a3fom"
},
"uid": "20210915-67102",
"type": "N1",
"foobar": [
{
"uid": "STK-00117",
"nom": "JOHN DOE T1"
},
{
"uid": "STK-00114",
"nom": "JOHN DOE T5"
}
]
},
{
"_id": {
"$oid": "6152da109e0ced07f16a3fxs"
},
"uid": "20210915-67108",
"type": "N5",
"foobar": [
{
"uid": "STK-00117",
"nom": "JOHN DOE T3"
},
]
},
{
"_id": {
"$oid": "6152da109e5ced07f16a3fdc"
},
"uid": "20210915-67108",
"type": "N12",
"foobar": [
{
"uid": "STK-00115",
"nom": "JOHN DOE T4"
},
{
"uid": "STK-00117",
"nom": "JOHN DOE T10"
},
{
"uid": "STK-00113",
"nom": "JOHN DOE T18"
},
]
},
{
"_id": {
"$oid": "6152da609e0ced07f16a3fdc"
},
"uid": "20210915-67108",
"type": "N17",
"foobar": [
{
"uid": "STK-00113",
"nom": "JOHN DOE T15"
},
{
"uid": "STK-00112",
"nom": "JOHN DOE T16"
},
{
"uid": "STK-00111",
"nom": "JOHN DOE T17"
},
]
},
{
"_id": {
"$oid": "6152da109e0ced07f16a3f5e"
},
"uid": "20210915-67108",
"type": "N16",
"foobar": [
{
"uid": "STK-00999",
"nom": "JOHN DOE T99"
},
]
}]
這是我的服務功能:
public List<Produit> GetFromSitesAsync(List<string> productSites)
{
// WORKING BUT EMPTY
FilterDefinition<Produit> filter = new BsonDocument(
"foobar.uid",
new BsonDocument(
"$in",
BsonArray.Create(productSites)
)
);
return _produits.Find(filter).ToList();
}
注意:
productSites = ["STK-00112","STK-00117","STK-00113","STK-00114"]
您可以使用BsonDocument
對象設置filter
,如下所示:
string[] uids = new string[] { "STK-00113", "STK-00117", "STK-00113", "STK-00114" };
FilterDefinition<Foo> filter = new BsonDocument(
"foobar.uid",
new BsonDocument(
"$in",
BsonArray.Create(uids)
)
);
var findFluent = collection.Find(filter);
或者
編寫 MongoDB 查詢並反序列BsonDocument
。
var query = @"{
'foobar.uid': {
$in: [
'STK-00113',
'STK-00117',
'STK-00113',
'STK-00114'
]
}
}";
var filter = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(query);
var findFluent = collection.Find(filter);
僅供參考,您可以使用 MongoDB Compass 將 Query 導出為 C# Language 。
var range = new[]
{
"STK-00113",
"STK-00117",
"STK-00113",
"STK-00114"
};
var client = new MongoClient();
var db = client.GetDatabase("d");
var coll = db.GetCollection<Foo>("c");
var filter = Builders<Foo>.Filter.Where(foo => foo.Bars.Any(b => range.Contains(b.uid)));
var result = coll.Find(filter).ToList();
生成的查詢將是:
{
"find": "c",
"filter": {
"Bars": {
"$elemMatch": {
"uid": {
"$in": ["STK-00113", "STK-00117", "STK-00113", "STK-00114"]
}
}
}
}
}
更新:我使用 shell 插入了您在原始問題中提到的數據,並且可以正常工作:
MongoDB Enterprise replset:PRIMARY> db.c.runCommand({
... "find": "c",
... "filter": {
... "foobar": {
... "$elemMatch": {
... "uid": {
... "$in": ["STK-00113", "STK-00117", "STK-00113", "STK-00114"]
... }
... }
... }
... }
... })
{
"cursor" : {
"firstBatch" : [
{
"_id" : ObjectId("616aca60e565e50be00b8a4a"),
"uid" : "20210915-67102",
"type" : "N1",
"foobar" : [
{
"uid" : "STK-00117",
"nom" : "JOHN DOE T1"
},
{
"uid" : "STK-00114",
"nom" : "JOHN DOE T5"
}
]
},
{
"_id" : ObjectId("616aca60e565e50be00b8a4b"),
"uid" : "20210915-67108",
"type" : "N5",
"foobar" : [
{
"uid" : "STK-00117",
"nom" : "JOHN DOE T3"
}
]
},
{
"_id" : ObjectId("616aca60e565e50be00b8a4c"),
"uid" : "20210915-67108",
"type" : "N12",
"foobar" : [
{
"uid" : "STK-00115",
"nom" : "JOHN DOE T4"
},
{
"uid" : "STK-00117",
"nom" : "JOHN DOE T10"
},
{
"uid" : "STK-00113",
"nom" : "JOHN DOE T18"
}
]
},
{
"_id" : ObjectId("616aca60e565e50be00b8a4d"),
"uid" : "20210915-67108",
"type" : "N17",
"foobar" : [
{
"uid" : "STK-00113",
"nom" : "JOHN DOE T15"
},
{
"uid" : "STK-00112",
"nom" : "JOHN DOE T16"
},
{
"uid" : "STK-00111",
"nom" : "JOHN DOE T17"
}
]
}
],
"id" : NumberLong(0),
"ns" : "so1.c"
},
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1634388656, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1634388656, 1)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.