我需要从“followingList.username”获取所有用户名并与之比较 posts'usernames,如果有任何匹配需要将那个添加到数组中。
Person Model
{
"_id" : ObjectId("554f20f5c90d3c7ed42303e1"),
"username" : "fatihyildizhan",
"followingList" : [
{
"_id" : ObjectId("55505b6ca515860cbcf7901d"),
"username" : "gumusluk",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58505b6ca515860cbcf7901d"),
"username" : "yalikavak",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58305b6ca515860cbcf7901d"),
"username" : "gumbet",
"avatar" : "avatar.png"
}
]
}
Post Model
{
"_id" : ObjectId("554f2df2a388R4b425b89833"),
"username" : "yalikavak",
"category" : "Summer",
"text" : "blue voyage with yacht"
},
{
"_id" : ObjectId("554f2df2a388P4b425b89833"),
"username" : "yalikavak",
"category" : "Winter",
"text" : "is coming ..."
},
{
"_id" : ObjectId("554f2df2a388K4b425b89833"),
"username" : "gumbet",
"category" : "Fall",
"text" : "there are many trees"
}
我尝试获取结果代码块,如下所示,但无法弄明白。
var filter = Builders<Post>.Filter.AnyEq("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
你能帮我解决这个问题吗? 感谢。
答案 0 :(得分:2)
逻辑被翻转,如果我正确理解你的用例,你需要的是$in
query:
var filter = Builders<Post>.Filter.In("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
在您的情况下,username
是一个简单的字段,您希望与候选列表进行匹配。 AnyEq
用于检查从嵌入的复杂对象列表中,至少有一个符合标准。这仍然转化为MongoDB中的简单查询,但需要进入&#39;需要更复杂语法的对象。