获取 属性 与嵌套在数组中的值匹配的所有对象
Get all objects where a property matches a value nested in array
我有以下数据table:
{
"_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 个对象(游戏和交互),其中游戏有一系列交互。
我仍然可以使用 mongocsharpdriver 执行此操作吗?
在此先感谢您的帮助。
希望它对你有用谢谢:-)
collection.Find(x => x.owner == "testuser").ToList(); //where collection is MongoDB collection
感谢您的所有建议,抱歉回复晚了。但是我找到了这样解决它的方法:
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();
这将 return 以特定用户为所有者的所有交互。
希望我能帮助别人解决这个问题。
我有以下数据table:
{
"_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 个对象(游戏和交互),其中游戏有一系列交互。
我仍然可以使用 mongocsharpdriver 执行此操作吗?
在此先感谢您的帮助。
希望它对你有用谢谢:-)
collection.Find(x => x.owner == "testuser").ToList(); //where collection is MongoDB collection
感谢您的所有建议,抱歉回复晚了。但是我找到了这样解决它的方法:
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();
这将 return 以特定用户为所有者的所有交互。 希望我能帮助别人解决这个问题。