我试图基于其_id字段从数组中删除对象,如下所示:
(posts是一个对象数组)
req.body.postID
但是,即使它们具有User
引用的不同_id值,它也会删除数组中的所有帖子。
注意:如果我使用不同的字段(如帖子的名称)尝试此相同的查询,那么它可以正常工作并删除该帖子。但是我需要通过_id字段来完成此操作以确保唯一性。
以下是 let userSchema = new mongoose.Schema({
id: String,
displayName: String,
posts: [
{
url: String,
description: String,
likes: [String]
}
]
});
模型的样子:
(我没有明确地将_id字段自动分配一个)
func showAlertWithCompletion(message:String,okTitle:String,cancelTitle:String?,completionBlock:@escaping (_ okPressed:Bool)->()){
let alertController = UIAlertController(title: AppName, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: okTitle, style: .default) { (ok) in
completionBlock(true)
}
alertController.addAction(okAction)
if let cancelTitle = cancelTitle{
let cancelOption = UIAlertAction(title: cancelTitle, style: .cancel, handler: { (axn) in
completionBlock(false)
})
alertController.addAction(cancelOption)
}
if let topController = UIWindow.topViewController(){
topController.present(alertController, animated: true, completion: nil)
}
}
为什么会发生这种情况?什么是可能的解决方案?