【问题标题】:Moogose complex query joinsMongoose 复杂查询连接
【发布时间】:2016-02-27 18:11:43
【问题描述】:

我在 mongoose 中遇到了一些复杂的查询连接(使用 nodejs)

我有模型用户

{  active_payment: {type: mongoose.Schema.Types.ObjectId, ref: 'Payment'}}

以及型号支付:

{status : Number, creation_date: Date}

我想找到所有状态为 2 的用户。 我试过了:

User.find({'active_payment.status': 2,}).populate('active_payment')

但它不起作用。有什么方法可以做到这一点,而不必通过 for 循环对所有用户进行排序? 我还想按付款的创建日期对用户进行排序。

【问题讨论】:

    标签: node.js mongodb sorting join mongoose


    【解决方案1】:

    User.find()无法直接联系到status,请试试这个

    User
       .find({})
       .populate({
              path: 'active_payment',
              match: { status: 2},
       })
       .exec(function(err, users) {
           users = users.filter(function(user) {
              return user.active_payment; // filter the user by active_payment field 
           });
       });
    

    match过滤status2填充文档,然后过滤user通过active_payment填充。

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 2011-10-21
      • 2010-11-02
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      相关资源
      最近更新 更多