mongoose MongoServerError:表达式$in正好有两个参数,1已传入

8xiog9wr  于 2023-05-07  发布在  Go
关注(0)|答案(1)|浏览(94)
const { cityid,areaid,symptomid } = req.params
Symptoms.findOne({symptomid: symptomid}, (err, suc) => {
console.log(suc);
if (!suc) {
  return res.status(404).json({ error: "Symptom not found", 
status: 
false });
}
else {
  masterClinic.aggregate([
    {
      $match:{
        cityid:cityid,
      }
    },
    {
      $project: {
        clinicid: 1,
        order: {
          $cond: {
            if: { $eq: ["$areaid", areaid] },
            then: 0,
            else: 1
          }
        }
      }
    },
    { $sort: { order: 1 } },
    { $project: { _id: 0, clinicid: 1 } },
    {
      $lookup: {
        from: "clinicdoctorschemas",
        localField: "clinicid",
        foreignField: "clinicid",
        as: "doctors",
      },
    },
    {
      $unwind: "$doctors"
    },
    {
      $replaceRoot: {
        newRoot: "$doctors"
      }
    },
    {
      $project: {
        _id: 0
      }
    },
    {
      $lookup: {
        from: "doctormasters",
        // localField: "doctorid",
        // foreignField: "doctorid",
        let: { doctorid: "$doctorid", departmentids: 
      suc.departmentids },
           pipeline: [
              { $match:
                 { $expr:
                    { $and:
                       [
                         { $eq: [ "$doctorid",  "$$doctorid" ] },
                         { $in: [ "$departmentids", 
    "$$departmentid" ] }
                       ]
                    }
                 }
              },
           ],
        as: "doctor",
      },
    }, {
      $unwind: "$doctor"
    },
    {
      $replaceRoot: {
        newRoot: "$doctor"
      }
    },
    {
      $project: {
        _id: 0
      }
    },
  ]).sort({ dob: 1 }).then((succ, error) => {
    if (succ) {
      return res.json({ data: succ, status: true })
    } else {
      return res.status(404).json({ error: "City Id not found.", 
    status: false });
    }
  })
}
})

这是此问题trying to join 4 schemas in MongoDB, but unable to get desired output的查询。
我试图解决这个问题,但得到这个错误。这是我的API。我不明白为什么它显示“MongoServerError:表达式$in正好有两个参数。1被传入。”调用此API时???语法错误也未显示。

rmbxnbpk

rmbxnbpk1#

经过几个小时的工作终于得到了输出。

const { cityid,areaid,departmentid } = req.params
masterClinic.aggregate([
{
  $match:{
    cityid:cityid,
  }
},
{
  $project: {
    clinicid: 1,
    order: {
      $cond: {
        if: { $eq: ["$areaid", areaid] },
        then: 0,
        else: 1
      }
    }
  }
},
{ $sort: { order: 1 } },
{ $project: { _id: 0, clinicid: 1 } },
{
  $lookup: {
    from: "clinicdoctorschemas",
    localField: "clinicid",
    foreignField: "clinicid",
    as: "doctors",
  },
},
{
  $unwind: "$doctors"
},
{
  $replaceRoot: {
    newRoot: "$doctors"
  }
},
{
  $project: {
    _id: 0
  }
},
{
  $lookup: {
    from: "doctormasters",
    // localField: "doctorid",
    // foreignField: "doctorid",
    let: { doctorid: "$doctorid", departmentid:departmentid  },
       pipeline: [
          { $match:
             { $expr:
                { $and:
                   [
                     { $eq: [ "$doctorid",  "$$doctorid" ] },
                     { $eq: [ "$departmentid", "$$departmentid" ] }
                   ]
                }
             }
          },
       ],
    as: "doctor",
  },
}, {
  $unwind: "$doctor"
},
{
  $replaceRoot: {
    newRoot: "$doctor"
  }
},
{
  $project: {
    _id: 0
  }
},
]).sort({ dob: 1 }).then((succ, error) => {
if (succ) {
  return res.json({ data: succ, status: true })
} else {
  return res.status(404).json({ error: "City Id not found.", status: false 
});
}
})

相关问题