$pull 适用于 mongo shell 但不适用于 nodejs(expressjs) 代码

$pull works on mongo shell but not in nodejs(expressjs) code

该集合包含以下文档:

{
"_id" : ObjectId("5b02df7a45b504151cb42c40"),
"email" : "micro@gmail.com",
"appointments" : [ 
    {
        "patient_name" : "patient_1",
        "patient_id" : 1.0,
        "time" : 1528915447.0
    }, 
    {
        "patient_name" : "patient_2",
        "patient_id" : 2.0,
        "time" : 1529915447.0
    }
]}

当我在 robomongo 工具或 mongo shell

db.getCollection('doctors').update(
{ email: "micro@gmail.com" },
{ $pull: { appointments: {patient_name: "patient_2", patient_id:2, time: 1529915447} } } 
)

但是,当我对节点 api 尝试同样的操作时,它只是将响应发送为 {success: true} 但没有从数据库

中删除该特定文档
app.post('/secure/doctor/reject_appointment', function(req, res){
    console.log(req.body)
    if(!req.user){
        res.json({success: false,message:"Unauthorized"});;
    return;
    }
    Doctor.findByIdAndUpdate(req.user.id,
        {$pull: { appointments: {patient_name: req.body.patient_name, patient_id:req.body.patient_id, time: req.body.time}}},
        {safe: true, upsert: true},
        function(err, doc) {
            if(err){
            console.log(err);
            res.json({
                success:false
                }).end();
            }else{
                console.log(doc.appointments)
                res.json({
                success:true
                }).end();
            }
        }
    );
});

请求正文:{ patient_name: 'patient_2', patient_id: 2, time: 1529915447 }找不到问题。

请更新 MongoDB 版本,只有一个唯一参数可以提取数据,您的查询将有效:另外,保持 NPM 和节点更新。

Upgrade MongoDB version to 3.2.

app.post('/secure/doctor/reject_appointment', function(req, res){
    console.log(req.body)
    if(!req.user){
        res.json({success: false,message:"Unauthorized"});;
    return;
    }
    Doctor.findByIdAndUpdate(req.user.id,
        {$pull: { appointments: {patient_id:req.body.patient_id}}},
        {safe: true, upsert: true},
        function(err, doc) {
            if(err){
            console.log(err);
            res.json({
                success:false
                }).end();
            }else{
                console.log(doc.appointments)
                res.json({
                success:true
                }).end();
            }
        }
    );
});

希望对您有所帮助。