如何通过 MongoDb 驱动程序按对象的对象 属性 查询
How to query by object's object property with MongoDb Driver
我正在做一个 .net 核心网络 api 使用 mongodb 驱动程序来访问我的数据库。
我有这个文档的结构
{
"_id" : 1,
"ParticipantIdentities" : [
{
"ParticipantId" : 1,
"Player" : {
"AccountId" : 45678945,
"AnotherProps" :"values"
}
}
[Another arrays items]
}
我想使用 object.ParticipantIdentities.Player.AccountId
我这样试过
var filter = Builders<Match>.Filter.Where(e => e.ParticipantIdentities.Where(p => p.Player.AccountId == AccountId).Count() > 1);
var result = await _context.Matches.Find(filter).ToListAsync();
它抛出了一个异常,说 .Count() 不受支持。
我还没有 mongodb 个查询。
提前致谢。
您只需
var filter = Builders<Match>.Filter.Eq("ParticipantIdentities.Player.AccountId", accountId);
我正在做一个 .net 核心网络 api 使用 mongodb 驱动程序来访问我的数据库。
我有这个文档的结构
{
"_id" : 1,
"ParticipantIdentities" : [
{
"ParticipantId" : 1,
"Player" : {
"AccountId" : 45678945,
"AnotherProps" :"values"
}
}
[Another arrays items]
}
我想使用 object.ParticipantIdentities.Player.AccountId
我这样试过
var filter = Builders<Match>.Filter.Where(e => e.ParticipantIdentities.Where(p => p.Player.AccountId == AccountId).Count() > 1);
var result = await _context.Matches.Find(filter).ToListAsync();
它抛出了一个异常,说 .Count() 不受支持。 我还没有 mongodb 个查询。
提前致谢。
您只需
var filter = Builders<Match>.Filter.Eq("ParticipantIdentities.Player.AccountId", accountId);