Qaz
Qaz

Reputation: 1576

MongoDB: Update all fields

Is there a way I can update every field in a MongoDB document without just listing all the fields, which might change later? Something like this?

db.update(
    {foo:"bar"},
    {$unset: {{}:""}}
)

Upvotes: 1

Views: 378

Answers (1)

wdberkeley
wdberkeley

Reputation: 11671

Consider the cogency of an operation like this. How are you updating all of the fields meaningfully without considering their specific types or values? It looks like you are trying to unset the fields or set them to a default, null-like value. What does it mean to unset every field in a document? Isn't it really just removing the document? Are you excluding _id and other immutable fields (shard key!) somehow? What is the default value for the possible field types? What is the default value for a field that can be an integer or an array of integers in a data model? It's also dangerous to try to do something like this, especially with flexible schemas. Needing to do the above operation indicates a good chance that you need to rethink your data model.

Upvotes: 1

Related Questions