如何使用MongoDb Driver按对象的对象属性进行查询

时间:2018-02-08 14:23:24

标签: mongodb .net-core asp.net-core-mvc mongodb-.net-driver asp.net-core-webapi

我正在使用mongodb驱动程序访问我的数据库.net核心web api。

我有这个文件的结构

{
"_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查询noob。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您需要做的就是

var filter = Builders<Match>.Filter.Eq("ParticipantIdentities.Player.AccountId", accountId);