我有一個架構如下(簡化):試圖刪除在貓鼬子文檔給了我一個內部貓鼬錯誤
var Permission = new Schema({
_id: String, // email address
role: String // "admin" or "member"
});
var Org = new Schema({
name: {type: String, index: {unique: true, dropDups: true}, trim: true},
permissions: [Permission]
});
一個例子文件將是這樣的:
{
"name": "My Org",
"permissions" : [
{"_id" : "[email protected]", "role" : "admin"},
{"_id" : "[email protected]", "role" : "member"}
]
}
我試圖刪除權限行之一,使用命令org.permissions.remove(req.params.email)
,如示於下面的上下文中:
exports.removePermissions = function(req, res) {
var name = req.params.name;
return Org
.findOne({name: name})
.select()
.exec(function(err, org) {
if (err) return Org.handleError(res, err);
if (!org) return Org.handleError(res, new Error("#notfound " + name));
org.permissions.remove(req.params.email);
org.save(function(err, org) {
if (err) return Org.handleError(res, err);
else return res.send(org);
});
});
};
當我這樣做,我得到以下錯誤:
TypeError: Cannot use 'in' operator to search for '_id' in [email protected]
at EmbeddedDocument.Document._buildDoc (/../node_modules/mongoose/lib/document.js:162:27)
at EmbeddedDocument.Document (/../node_modules/mongoose/lib/document.js:67:20)
at EmbeddedDocument (/../node_modules/mongoose/lib/types/embedded.js:27:12)
at new EmbeddedDocument (/../node_modules/mongoose/lib/schema/documentarray.js:26:17)
at MongooseDocumentArray._cast (/../node_modules/mongoose/lib/types/documentarray.js:62:10)
at Object.map (native)
at MongooseDocumentArray.MongooseArray.remove (/../node_modules/mongoose/lib/types/array.js:360:21)
at model.Org.methods.removePermissions (/../models/org.js:159:20)
我能想到的唯一的事情就是貓鼬不支持_id字段不是對象ID的?這很奇怪,因爲我在代碼中的其他地方使用了它們,並且工作正常(例如org.permissions.id(「[email protected]」))。
任何建議非常感謝!
再次 - 謝謝!我將這個remove()問題添加到Github上的mongoosejs問題列表中:https://github.com/LearnBoost/mongoose/issues/1278 –
現在問題已經修復了......但它仍然處於預發佈階段,3.6科。雖然它是一個有點大的bug ...似乎我做出了不好的選擇,在平原蒙戈上選擇貓鼬...... – Marius
感謝您的支持,讓這個操作變得非常棒。唯一的問題是你不知道'$ pull'操作是否成功,即,如果DB中有一個Permission對象被刪除了。對於這個錯誤檢查級別,你需要用'findById'分兩步完成,然後在結果返回的'org'上進行操作。 – fiznool