取消设置数组元素

时间:2013-02-01 17:45:24

标签: arrays mongodb

是否可以在数组字段上使用$ unset运算符并删除与查询匹配的元素。例如,我试图从字段“文件”数组中删除35。

{
  _id : 1,
  files : [1,12,35,223]
}
// Ive tried this but it does not work
db.col.update({_id : 1}, {$unset : { files : 35}})
// or this does not work
db.col.update({_id : 1}, {$unset : { "files.35" : 1}})

2 个答案:

答案 0 :(得分:2)

你试过$pull operator吗?如:

db.col.update({_id: 1}, {$pull: {files: 35}})

答案 1 :(得分:0)

您可以改为使用$pull

db.col.update({ _id : 1 }, {$pull: { files : 35 } })