Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 292 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deleting a key/value from existing MongoDB entry

#1
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.

Long story short, how can I turn

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

into

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

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

Reply

#2
Also think about `upsert()` that will insert your row as a new document if `_id` does not exist or update the existing document if any.
Reply

#3
To reference a package and remove various "keys", try this

db['name1.name2.name3.Properties'].remove([ { "key" : "name_key1" }, { "key" : "name_key2" }, { "key" : "name_key3" } )]
Reply

#4
Try [`$unset`][1] in a call to `update()`.

Like this:

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

And, as [vikneshwar][2] 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} })

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through