我有如下模式(简化):
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" : "joe@gmail.com", "role" : "admin"},
{"_id" : "mary@gmail.com", "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 joe@gmail.com
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)
我唯一能想到的是 Mongoose 不支持不是 ObjectID 的 _id 字段?这很奇怪,因为我在我的代码中的其他地方使用了这些并且它工作正常(例如 org.permissions.id("joe@gmail.com") 工作)。
任何建议都非常感谢!