$ pull работает в оболочке mongo, но не в коде nodejs (expressjs) - PullRequest
0 голосов
/ 27 мая 2018

Коллекция содержит следующий документ:

{
"_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
    }
]}

Следующая операция удалит документ с Patient_Name = "Patient_2" (т. Е. Второй документ), когда я попробовал его на инструменте 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 } Не могу понять проблему.

1 Ответ

0 голосов
/ 27 мая 2018

Пожалуйста, обновите версию MongoDB, и только один уникальный параметр может извлечь данные, и ваш запрос будет работать: Кроме того, сохраняйте обновление NPM и Node.

Обновите версию MongoDB до 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();
            }
        }
    );
});

Надеюсь, это поможет.

...