【问题标题】:Condition on Mongoose $LookupMongoose $Lookup 的条件
【发布时间】:2020-11-02 23:50:22
【问题描述】:

我正在尝试加入 Mongoose。

我有两个收藏

  1. 大师挑战

  2. 挑战历史

我的挑战历史包含一个归档 ma​​ster_id,它是对 Master Challenge 的 _id 的引用

大师挑战模型

const mongoose = require('mongoose');

const masterChallenge = mongoose.Schema({
  title:{
    type:String,
  },
  descripton:{
    type:String,
  },
  challengeType:{
    type:Number,
  },
  
});

module.exports = mongoose.model('masterChallenge',masterChallenge);

挑战历史模型

const mongoose = require('mongoose');

const challangeHistory= mongoose.Schema({

  master_id:{
    type: mongoose.Schema.Types.ObjectId,
    ref:'masterchallenges'
  },
  host_user_id:{
    type: mongoose.Schema.Types.ObjectId,
    ref:'useraccounts'
  },
  compete_with_id:{
    type: mongoose.Schema.Types.ObjectId,
    ref:'useraccounts'
  },
  adjucator_id:{
    type: mongoose.Schema.Types.ObjectId,
    ref:'useraccounts'
  },
  status:{
    type:Number,
  },
  created_at:{
    type:Date,
  }
});

module.exports = mongoose.model('challangeHistory',challangeHistory);

id = 'user _id'(用于以下段落)

我想通过 Master Challenge 执行 join 以仅从 Master Challenge 中获取具有 host_user_id==idcompete_with_id==idadjucator_id 的记录==id 如果我的主挑战类型为 1 否则挑战状态为 0 那么它不应该对 host_user_idcompete_with_idadjucator_id 执行任何操作strong> 并返回带有 Challenge 0 的文档。

我的查询

findMasterOnHistory:async function(id){

let dbResponse = await MASTER_CHALLENGE.aggregate([
       {$lookup:{from:'challangehistories',localField:'_id',foreignField:'master_id',as:'challangehistories'}},
     ]);
   
return dbResponse;
}

我尝试了很多似乎不起作用的东西,但我不知道如何在 $lookup 上添加条件。

【问题讨论】:

标签: node.js mongodb mongoose


【解决方案1】:

您可以在$lookup 阶段使用管道:

例如,您的 host_user_id 存储在我称之为 hostUserId 的变量中。

let dbResponse = await MASTER_CHALLENGE.aggregate([{
                         $lookup: {
                            from: 'challangehistories',
                            'let': {masterId: '$_id'},
                            pipeline: [{
                              $and: [{
                                $eq: ['$master_id', '$$masterId']
                              }, {
                                $eq: ['$host_user_id', hostUserId]
                              }]
                            }],
                            as: 'challangehistories'
                         }
                       }])

【讨论】:

    【解决方案2】:

    这是我的问题的答案。 我在 $lookUp 中使用了 管道

    1. 已将 _idMaster Challenge 保存到 ma​​ster
    2. 执行 $and 以匹配 挑战历史的 -> ma​​ster_id_id [ 挑战大师的$$master ]
    3. $and 内执行 $or 以获取基于 host_user_idcompete_with_id 或 adjucator_id
    {
      from: 'challangehistories',
      let: {
                   master:"$_id",
                   
                 },
       pipeline:[
                    {
                      $match:{
                        $expr:{
                          $and:[
                            {$eq:["$$master","$master_id"]},
                            {$or:[
                                {
                                  $or:[
                                      {$eq:["$host_user_id",ObjectId(id)]},
                                      {$eq:["$compete_with_id",ObjectId(id)]},
                                      {$eq:["$adjucator_id",ObjectId(id)]},
                                    ]
                                }
                                
                              ]},
                          ],
                        },
                        
                        
                      }
                    },
                 ],
      as: 'challangehistories'
    }
    
    

    【讨论】:

      猜你喜欢
      • 2021-01-25
      • 2020-10-14
      • 2018-08-16
      • 2021-07-24
      • 2019-05-07
      • 1970-01-01
      • 2020-09-14
      • 2021-08-25
      • 1970-01-01
      相关资源
      最近更新 更多