简体   繁体   English

如果存在与 MongoDB 和 Mongoose 匹配的另一个文档,则添加字段

[英]Add field if another document matching exist with MongoDB & Mongoose

I have in my DB a collection of Posts with basic informations like:我的数据库中有一组包含基本信息的帖子,例如:

Posts = {
   _id: '',
   title '',
   desc '',
...
}

i also have a Likes collection like (document are created or deleted of user like the post):我也有一个喜欢的集合,比如(创建或删除用户喜欢的文件):

Likes = {
_id: '',
user_id: '',
post_id: '',
}

and the goal would be that when i get a list of Posts , to add a field Liked who show true/false if a document Like match or not, and also the number of match in the Like collection (this i know how to) like:目标是,当我获得Posts列表时,添加一个字段Liked ,如果文档Like匹配,则显示 true/false,以及Like集合中的匹配数(我知道如何)喜欢:

Posts = [{
   _id: '',
   title '',
   desc '',
   liked: true,
   likes: 5,
...
}, ...]

i've used $lookup & $addFields but can join only with one value so far.我已经使用$lookup$addFields但到目前为止只能加入一个值。 In my case i want one specific document and filtered by 2 fields (user_id, post_id).在我的情况下,我想要一个特定的文档并按 2 个字段(user_id、post_id)过滤。

What i get so far:到目前为止我得到了什么:

    const posts = await posts.aggregate
    ([
      {
        $lookup: {
          from: 'likes',
          localField: '_id',
          foreignField: 'post_id',
          as: 'likes'
        }
      },
      { $match: {
        user_id: mongoose.Types.ObjectId(req.params.id),
      } },
      {
        $addFields: {
          likes: 
            { $size: "$likes" } // here nb of likes
        }
      }
    ])

Try with this one试试这个

db.getCollection('post').aggregate([
  {
    $lookup: {
      from: 'likes',
      localField: '_id',
      foreignField: 'post_id',
      as: 'likes'
    }
  },
  {
      $addFields:{
          size:{$size:"$likes"},
          liked: 
          {$size: {
                        $filter: {
                            input: "$likes",
                            as: "item",
                            cond: { $eq: [ "$$item.user_id", ObjectId("5f2a38ad61a591cf96d58f6a") ] }
                        }
                }
             }
          }
  },
  {
  $addFields:{
      liked:{
          $cond:[{$eq:["$liked",0]},false,true]
          }
      }    
  }
])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mongoose 从另一个文档向一个文档添加一个字段 - mongoose add a field to a document from another document Mongodb mongoose - 如果字段不存在,如何将文档添加到数组 - Mongodb mongoose - How to add a document to an array if not present by a field 向现有MongoDB文档添加字段(在Node.js中使用Mongoose) - Add a field to existing MongoDB document (with Mongoose in Node.js) 将一个新字段添加到集合的所有文档中,将文档字段中的值添加到 MongoDB (Mongoose) 中,记录为 300K+ - Add a new field to all documents of a collection with the value from the document field into MongoDB (Mongoose) with records of 300K+ 如何使用带有严格:false模式的猫鼬将字段添加到mongoDB文档中? - How does one add a field to a mongoDB document using mongoose with a strict : false schema? 如何检查文档是否存在与mongodb中的特定字段 - How to check if document exist with particular field in mongodb 向文档mongodb添加新字段 - Add a new field to a document mongodb MongoDB 查找匹配文档并更新对象数组中的匹配字段 - MongoDB find matching document and update matching field inside array of objects Mongoose-MongoDB-Express:在嵌入式文档中深3级的字段中推送数据 - Mongoose - MongoDB - Express: Pushing data in a field that is 3 levels deep in a embedded document 具有多个字段的Mongoose查询文档,包括子字段Mongodb - Mongoose query document with multiple fileds including sub field Mongodb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM