简体   繁体   English

嵌入式数组mongodb上的$ pull / $ pop

[英]$pull/$pop on embedded array mongodb

I have a simple collection with the following schema 我有一个具有以下架构的简单集合

{
 name:"John",
 brands:[
 {
  name:"some",
  email:"asdf@some.com"
 },
 {
  name:"blah"
  email:"blah@blah.com"
 }
]
}

i'm using the following query to remove the embedded object inside my brands array field: 我正在使用以下查询来删除我的品牌数组字段内的嵌入式对象:

var args = {
'query':{name:"John",brands.email:"asdf@some.com"}
,update:{
'$pull':{
 'brands.$.email:"asdf@some.com"
}
}
}

i'm using nodejs driver for mongodb and when i run the above using following: 我正在为mongodb使用nodejs驱动程序,当我使用以下命令运行上述代码时:

collectionName.findAndModify(args,function(req,res){

})

I get the following error: 我收到以下错误:

MongoError: Cannot apply $pull/$pullAll modifier to non-array MongoError:无法将$ pull / $ pullAll修饰符应用于非数组

I guess i'm doing correct but still getting this error. 我想我做得正确,但仍然收到此错误。 Any help appreciated. 任何帮助表示赞赏。

Your $pull is targeting email which isn't an array. 您的$pull定位到不是数组的email If you're trying to remove the matching element, you can do it like this: 如果您要删除匹配的元素,则可以这样操作:

var args = {
  query: {name: "John"},
  update: {
    '$pull': {
      brands: {email: "asdf@some.com"}
    }
  }
}

or if you're trying to remove the email field, use $unset instead: 或者,如果您要删除email字段,请改用$unset

var args = {
  query: {name: "John", "brands.email": "asdf@some.com"},
  update: {
    '$unset': {
      'brands.$.email': 1
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM