2
我是一名mongoDB菜鸟,无法使用Mongoose从MongoDB中的数组中删除团队对象。我的用户收藏看起来像;从MongoDB/MongoDB中的数组中删除一个对象
{ "orders":[],
"teams":[
{
"teamname":"Team One",
"_id":{"$oid":"566a038404ebcdc344c5136c"},
"members":[
{
"firstname":"Member ",
"lastname":"Three",`enter code here`
"email":"[email protected]",
"initials":"MT",
"_id":{"$oid":"566a038404ebcdc344c5136d"}
},
]
},
{
"teamname":"Team Two",
"_id":{"$oid":"566a07f404ebcdc344c51370"},
"members":[
{
"firstname":"Member",
"lastname":"Two",
"email":"[email protected]",
"initials":"MT",
"_id":{"$oid":"566a07f404ebcdc344c51371"}
},
]
},
{
"teamname":"Team Three",
"_id":{"$oid":"566a083504ebcdc344c51373"},
"members":[
{
"firstname":"Member",
"lastname":"Four",
"email":"[email protected]",
"initials":"MF",
"_id":{"$oid":"566a083504ebcdc344c51374"}
},
]
}
],
}
而我的清除路线看起来像;
exports.getTeamDelete = function(req, res) {
User.findById(req.user.id, function(err, user) {
if (err) return next(err);
var selectedTeamIndex;
for (i = 0; i < user.teams.length; i++){
if (user.teams[i]._id==req.params.teamid){
selectedTeamIndex=i;
break
}
}
console.log("before", user.teams)
console.log(req.params.teamid)
teamID=req.params.teamid;
userID=req.user.id;
User.update(
{'_id': userID},
{ $pull: { "teams" : { teamID } } },
{ multi: true },
function(err, data){
console.log(err, data);
}
);
user.markModified("teams");
user.save(function(err) {
console.log("after", user.teams)
if (err) return next(err);
req.flash('success', { msg: 'Team deleted' });
});
});
};
控制台在User.update之前和之后的日志完全相同,我也没有得到任何错误。 console.log(err,data)的输出结果为:
null { ok: 1, nModified: 0, n: 1 }
我真的不知道该如何解释。我试过也没有按照这里建议的传递userID。 https://stackoverflow.com/a/24289161/574869无效(相同的结果)。另外我已经尝试做用户更新为;
User.update(
{'_id': userID},
{ $pull: { "teams" : { id: teamID } } },
false,
true
);
这里提到https://stackoverflow.com/a/15641895/574869这导致和的误差;
/Users/markbreneman/Desktop/GatherRoundApp/node_modules/mongoose/lib/query.js:2022
oldCb(error, result ? result.result : { ok: 0, n: 0, nModified: 0 });
^
TypeError: oldCb is not a function
我也不确定如何解释。如果任何人对我做错了什么有所了解,或者如何轻松从我的团队阵列中移除团队,我会非常感激。