【问题标题】:Is there an opposite to mongoose populate() method? (Mongoose, node.js, express)mongoose populate() 方法有什么相反的吗? (猫鼬,node.js,快递)
【发布时间】:2019-05-19 02:50:20
【问题描述】:

我有两种猫鼬方案,一种用于用户,另一种用于帖子。

一个用户可以喜欢很多帖子。

最后,我想在客户端显示用户喜欢的所有帖子,在一个部分中我可以使用 populate() 方法,在另一部分中用户不喜欢的帖子不创建重复项喜欢的帖子。

是否有 unpopulate() 方法可以用来只获取不喜欢的帖子?如果没有,解决此问题的最佳方法是什么?

userScheme = 
   { 
    // some other fields…

    post: {
            type: Schema.Types.ObjectId,
            ref: 'Post'
        }
  }


postScheme = 
   { 
     // some other fields…

     user: {
            type: Schema.Types.ObjectId,
            ref: 'User'
        },
        likes: [
                {
                    type: Schema.Types.ObjectId,
                    ref: 'User'
                }
        ],

}

【问题讨论】:

    标签: node.js express mongoose routes


    【解决方案1】:

    您可以查询在其likes 数组中没有特定用户的所有帖子,即尚未喜欢该用户。 Reference

    Post.find({ likes: { $nin: [user._id] }})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-21
      • 2020-11-19
      • 2014-01-01
      • 1970-01-01
      • 2021-04-11
      • 2017-02-04
      • 2021-05-17
      • 2011-08-28
      相关资源
      最近更新 更多