【问题标题】:Delete an item in array in mongodb [duplicate]在mongodb中删除数组中的一个项目[重复]
【发布时间】:2020-03-26 05:31:03
【问题描述】:

我有一个文件

{ “_id”:ObjectId(“5de2a0a2336a4c5adc3fc25c”), “图片” : { “其他图像”:[ { "imageUrl" : "HL2370207N_1575195338958.jpg", “状态”:真 }, { "imageUrl" : "HL2370207N_1575195373805.jpg", “状态”:真 }, { "imageUrl" : "HL2370207N_1575195382992.jpg", “状态”:真 } ] } }

我必须删除 otherImages 数组的一项,其 imageUrl 为“HL2370207N_1575195382992.jpg” $pull 无法应用,因为它位于不是数组的节点图像下。请给予任何指导。我是 mongoDB 的新手

【问题讨论】:

  • 哼,为什么你觉得$pull不能申请?你能显示你的查询吗?
  • db.userdetails.update( { }, { $pull: { image: {OtherImages: { $elemMatch: { imageUrl: "HL2370207N_1575195382992.jpg" } } } }} 错误是 WriteResult ({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 2, "errmsg" : "Cannot apply $pull to a non-array value" } })

标签: node.js mongodb


【解决方案1】:

我认为您可以指定要删除的值。像这样:

{ $pull: { <field1>: <value|condition> } }

来源和例子:https://docs.mongodb.com/manual/reference/operator/update/pull/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 2018-10-23
    • 2014-08-08
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    相关资源
    最近更新 更多