我的文档是这样的
"page_elements" : [
{
"type" : "gallery",
"words" : [
"gallery",
"photos"
]
},
{
"type" : "cart",
"words" : [
"cart",
"shop"
]
},
{
"type" : "Privacy",
"words" : [
"privacy",
"policy",
"privacy policy"
]
},
{
"type" : "cart",
"words" : [
"cart",
"shopping cart",
"buy now",
"add to cart",
"add to basket"
]
}
不正确的是,我已经两次更新购物车。现在,我需要删除阵列中的第一个购物车,并保留其余文档。
在这里,我需要删除
{
"type" : "cart",
"words" : [
"cart",
"shop"
]
}
我试过了..
db.ds_master_language_words.remove( { "page_elements.type" : "cart" } )
您不想删除,您想更新。$ unset将摆脱该字段,并且您知道位置:
db.ds_master_language_words.update(
{ <your_document_id_match > },
{ $unset: { "page_elements.1": ""}}
)
它将在数组中设置为null,但是现在可以通过$ pull轻松删除:
db.ds_master_language_words.update(
{ <your_document_id_match > },
{ $pull: { "page_elements": null }}
)
另请参阅该update
方法的文档
http://docs.mongodb.org/manual/reference/method/db.collection.update/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句