Py学习  »  MongoDB

使用多个ID过滤MongoDB中的查询(内部数组)

Ajas Aju • 4 年前 • 246 次点击  
CommentCollection
{
 "_id":"5b63f0f23846b70011330889",
  "CommentType":"task",
  "EntityReferenceId":"6082ef25-6f9a-4874-a832-f72e0f693409",
  "Threads":[
  {
     "_id":"69bcef71-3695-4340-bdec-4a6e4c58c490",
     "CommentType":"task",
     "UserId":ObjectId("52ffc4a5d85242602e000000"),         
     "CommentByUserType":"Admin",
     "EntityReferenceId":"6082ef25-6f9a-4874-a832-f72e0f693409",
     "Content":"fdffd",

  },
  {
     "_id":"69bcef71-3695-4340-bdec-4a6e4c58c490",
     "CommentType":"task",
     "UserId":ObjectId("52ffc4a5d85242602e000000"),         
     "CommentByUserType":"Admin",
     "EntityReferenceId":"6082ef25-6f9a-4874-a832-f72e0f693409",
     "Content":"fdffd",

  }
 ]
}

在这里,我必须根据两个条件从ASP.NET核心编写MongoDB过滤器查询, 首先,我想通过 实体引用ID ,然后希望通过 身份证件 从第一个结果开始。

任何帮助都将不胜感激。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38524
 
246 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Ajas Aju
Reply   •   1 楼
Ajas Aju    5 年前

我得到了答案,我写了下面的方法

 public async Task<bool> UpdateCommentAsync(Threads thread)
    {
        var builder = Builders<Comments>.Filter;
        var filter = builder.Empty;
        var update = Builders<Comments>.Update.Set("Threads.$[i].Content", thread.Content);
        var arrayFilters = new List<ArrayFilterDefinition> { new JsonArrayFilterDefinition<Threads>("{'i._id': '" + thread.Id + "'}") };
        var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
        var result = await _context.Comments.UpdateManyAsync(filter, update, updateOptions);
        return result.ModifiedCount > 0;
    }