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
我也不確定如何解釋。如果任何人對我做錯了什麼有所瞭解,或者如何輕鬆從我的團隊陣列中移除團隊,我會非常感激。