该集合包含以下文档:
{
"_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}
但是没有从db中删除该特定文档
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 }
无法解决问题。
答案 0 :(得分:1)
请更新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();
}
}
);
});
希望这有帮助。