从现有的MongoDB条目中删除键/值 [英] Deleting a key/value from existing MongoDB entry

查看:132
本文介绍了从现有的MongoDB条目中删除键/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从特定集合中的每个条目中删除特定的键和值.我已经研究了remove,这似乎只适用于整个条目.在查看更新时,我不相信用null或空字符串更新特定键将达到我想要的目的.我是mongodb的初学者,所以请原谅我的无知.

I need to delete a certain key and value from every entry in a particular collection. I've looked into remove and that seems to be for only entire entries. Looking at update, I don't believe updating a particular key with null or an empty string would achieve what I'm trying to do. I'm very much a beginner with mongodb, so please excuse my ignorance.

长话短说,我该怎么转

{
  "_id" : 1234,
  "name" : "Chris",
  "description" : "Awesome"
}

进入

{
  "_id" : 1234,
  "name" : "Chris"
}

是否不删除条目并创建新条目,或使用任何非mongodb命令?谢谢!

without deleting the entry and creating a new one, or using any non-mongodb commands? Thanks!

推荐答案

尝试 $unset 调用update().

喜欢这个:

db.collection_name.update({ _id: 1234 }, { $unset : { description : 1} })

而且,正如 vikneshwar 所说,如果要从所有(或多个)文档中删除一个字段,则可以使用updateMany()像这样:

And, as vikneshwar commented, if you want to remove one field from all (or multiple) documents you can use updateMany() like this:

db.collection_name.updateMany({}, { $unset : { description : 1} })

这篇关于从现有的MongoDB条目中删除键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆