【发布时间】:2020-10-19 14:06:32
【问题描述】:
我试图从我的架构中的 array 中删除一个特定值(游戏),这是代码:
User.update({ userName: user }, { $pull: { games: { _id: deleteItem } } }, function (err, val) {
console.log(err);
});
架构:
const userSchema = new mongoose.Schema({
userName: { type: String, index: true, unique: true },
userPassword: String,
games: [gameSchema]
});
错误:
MongoError: E11000 重复密钥错误集合:mountain.users 索引:games.password_1 errmsg: 'E11000 重复密钥错误集合:mountain.users index: games.password_1 dup key: { games.password: null }', [符号(mongoErrorContextSymbol)]:{} }
为什么会出现错误,我该如何解决?/从对象内部的数组中删除值的其他方法
感谢您的帮助!
【问题讨论】:
-
请在帖子中包含以下内容:
db.collection.getIndexes()输出。 -
也许您有另一个文档,其中
games.password不存在。可以考虑删除索引:user.games.password。 -
@noam 我在所有游戏中都有密码
-
@prasad_ 我尝试对其进行控制台,但出现错误“TypeError: User.getIndexes is not a function”
-
@ORI,但是在您更新之后,这个特定游戏应该没有密码,对吧?这似乎已经发生了,有人将一个文档更新为
$pull密码远离它。