社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

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

Ajas Aju • 5 年前 • 312 次点击  
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
 
312 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Ajas Aju
Reply   •   1 楼
Ajas Aju    6 年前

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

 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;
    }