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



集合包含以下文档:

{
"_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外壳上尝试时带有patient_name="patient_2"的文档(即第二个文档(

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和节点更新。

将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();
}
}
);
});

希望这有帮助。

最新更新