获取属性与嵌套在数组中的值匹配的所有对象 [英] Get all objects where a property matches a value nested in array
问题描述
我有以下数据表:
{
"_id" : ObjectId("value"),
"owner" : "testuser",
"date" : ISODate("2017-03-16T12:45:03.386Z"),
"location" : "thuis",
"venue" : "bijna thuis",
"description" : "fghgfh",
"completed" : false,
"winnerName" : null,
"subscriptions" : [],
"interactions" : [
{
"_id" : ObjectId("objectid"),
"owner" : "testuser",
"type" : "guess",
"date" : ISODate("2017-03-06T12:13:10.049Z"),
"answer" : false,
"message" : "test 1"
},
{
"_id" : ObjectId("objectid"),
"owner" : "testuser",
"type" : "guess",
"date" : ISODate("2017-03-06T12:13:10.049Z"),
"answer" : false,
"message" : "test 2"
}
],
"__v" : 0,
"active" : true
}
以上只是一个游戏对象.这意味着我们在数据库中有几个这样的对象. 我试图只获得所有者=="testuser"的交互. 问题是我似乎无法找出最佳方法. 在我的代码中,我有2个对象(游戏与互动),其中游戏具有一系列互动.
The above is just one game object. Which means we got several of this objects in our database. I am trying to get only the interactions where the owner == "testuser". The problem is that I cannot seem to figure out the best way to do this. In my code I got 2 objects ( Game & Interaction) where Game has an array of interactions.
在某种程度上,我仍然可以使用mongocsharpdriver执行此操作.
Is there someway I can still do this using the mongocsharpdriver.
提前感谢所有帮助.
推荐答案
感谢所有建议,对您的回复表示抱歉.但我找到了解决此问题的方法:
Thanks for all the suggestions and sorry for the late response. But I find a way to solve it like this:
var filter = Builders<Game>.Filter.ElemMatch("interactions",
Builders<Interaction>.Filter.Eq("owner", owner));
var interactions = await MongoCollection.Find(filter).ToListAsync();
return interactions.SelectMany(item => item.Interactions).ToList();
这将返回具有某个用户作为所有者的所有交互. 希望我可以帮助别人解决这个问题.
This will it will return all the interactions that has a certain user as owner. Hope I can help somebody out with this answer.
这篇关于获取属性与嵌套在数组中的值匹配的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!