{
name: 'book',
tags: {
words: ['abc','123'],
lat: 33,
long: 22
}
}
假设这是一个文档。如何从该集合中的所有文档中完全删除“words
”?我希望所有文档都没有“words
”:
{
name: 'book',
tags: {
lat: 33,
long: 22
}
}
试试这个:如果你的收藏是“例子”
db.example.update({}, {$unset: {words:1}}, false, true);
参考这个:
http://www.mongodb.org/display/DOCS/Updating#Updating-%24unset
更新:
上面的链接不再涵盖“$ unset”。如果要从集合中的所有文档中删除此字段,请务必添加 {multi: true}
;否则,它只会从它找到的第一个匹配的文档中删除它。有关更新的文档,请参阅此:
https://docs.mongodb.com/manual/reference/operator/update/unset/
例子:
db.example.update({}, {$unset: {words:1}} , {multi: true});
一开始,我不明白为什么这个问题有赏金(我认为这个问题有一个很好的答案,没有什么可补充的),但后来我注意到这个被接受并投票了 15 次的答案实际上是错误的!
是的,您必须使用 $unset
operator,但此未设置将删除集合文档中不存在的 words 键。所以基本上它什么都不做。
因此,您需要告诉 Mongo 查看文档标签,然后使用 dot notation 查看单词。所以正确的查询是。
db.example.update(
{},
{ $unset: {'tags.words':1}},
false, true
)
只是为了完整起见,我将参考 another way of doing it,它更糟糕,但这样您可以使用任何自定义代码更改字段(甚至基于本文档中的另一个字段)。
db.example.updateMany({},{"$unset":{"tags.words":1}})
我们还可以使用它来更新多个文档。
false, true
作为最后两个参数传递给 update()
更清楚
在 MongoDB 中删除或删除字段
对于单个记录 db.getCollection('userData').update({}, {$unset: {pi: 1}})
对于多记录 db.getCollection('userData').update({}, {$unset: {pi: 1}}, {multi: true})
db.collection.updateMany({}, {$unset: {"fieldName": ""}})
updateMany
需要每个文档的匹配条件,因为我们传递 {} 它始终为真。第二个参数使用 $unset
运算符删除每个文档中的必填字段。
从 Mongo 4.2
开始,还可以使用稍微不同的语法:
// { name: "book", tags: { words: ["abc", "123"], lat: 33, long: 22 } }
db.collection.updateMany({}, [{ $unset: ["tags.words"] }])
// { name: "book", tags: { lat: 33, long: 22 } }
由于 update 方法可以接受聚合管道(注意方括号表示使用聚合管道),这意味着这里使用的 $unset
运算符是聚合运算符(与 "query" one 相对),其语法接受一个字段数组。
PyMongo(Python mongo)的解决方案:
db.example.update({}, {'$unset': {'tags.words':1}}, multi=True);
我试图做与此类似的事情,而是从嵌入式文档中删除该列。我花了一段时间才找到解决方案,这是我遇到的第一篇文章,所以我想我会在这里为其他尝试做同样事情的人发帖。
所以让我们说你的数据看起来像这样:
{
name: 'book',
tags: [
{
words: ['abc','123'],
lat: 33,
long: 22
}, {
words: ['def','456'],
lat: 44,
long: 33
}
]
}
要从嵌入文档中删除列 words
,请执行以下操作:
db.example.update(
{'tags': {'$exists': true}},
{ $unset: {'tags.$[].words': 1}},
{multi: true}
)
或使用 updateMany
db.example.updateMany(
{'tags': {'$exists': true}},
{ $unset: {'tags.$[].words': 1}}
)
$unset
只会在值存在时对其进行编辑,但它不会进行安全导航(它不会首先检查 tags
是否存在),因此嵌入文档需要存在。
这使用了 3.6 版中引入的 all positional operator ($[]
)
因为我在寻找使用 MongoEngine 删除字段的方法时一直在找到这个页面,所以我想在这里发布 MongoEngine 方式也可能会有所帮助:
Example.objects.all().update(unset__tags__words=1)
在 mongoDB shell 中,此代码可能会有所帮助:
db.collection.update({}, {$unset: {fieldname: ""}} )
默认情况下,update() 方法更新单个文档。设置多参数以更新所有符合查询条件的文档。
在 3.6 版中更改。句法 :
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
例子 :
db.getCollection('products').update({},{$unset: {translate:1, qordoba_translation_version:1}}, {multi: true})
在您的示例中:
db.getCollection('products').update({},{$unset: {'tags.words' :1}}, {multi: true})
而对于 mongomapper,
文件:关闭
要删除的字段:shutoff_type
Shutoff.collection.update( {}, { '$unset' => { 'shutoff_type': 1 } }, :multi => true )
要删除字段,您可以使用此命令(这将应用于集合中的所有文档):
db.getCollection('example').update({}, {$unset: {Words:1}, {multi: true});
并在一个命令中删除多个字段:
db.getCollection('example').update({}, {$unset: {Words:1 ,Sentences:1}}, {multi: true});
{名称:'书',标签:{词:['abc','123'],纬度:33,长:22}}
答:
db.tablename.remove({'tags.words':['abc','123']})
检查“单词”是否存在,然后从文档中删除
db.users.update({"tags.words" :{$exists: true}},
{$unset:{"tags.words":1}},false,true);
true 表示如果匹配则更新多个文档。
您也可以使用 3.4 的项目在聚合中执行此操作
{$project: {"tags.words": 0} }
要引用一个包并删除各种“键”,试试这个
db['name1.name2.name3.Properties'].remove([
{
"key" : "name_key1"
},
{
"key" : "name_key2"
},
{
"key" : "name_key3"
}
)]
tags.words
应该是$unset
,而不是words
?另见萨尔瓦多·达利的回答。updateMany
代替{multi:true}
,即db.example.updateMany({},{"$unset":{words:""}})