Ufuk UYSAL
Ufuk UYSAL

Reputation: 107

Update field and value mongoDB

Let say I have a collection. I want to change the field name and value in the whole collection like this

Current:

language:"en",
documentId:"123"

Desired:

  languages:["en"],
  documentIds:["123"]

db.foo.... Thank you in advance

Upvotes: 0

Views: 49

Answers (1)

Takis
Takis

Reputation: 8705

Query

  • empty filter {} and updateMany to update all collection
  • you can use "$field" to refer to filed and and array literals
  • and $unset to remove the old fields

*pipeline update requires MongodDB >= 4.2

Test code here

updateMany({},
 [{"$set": {"languages": ["$language"], "documentIds": ["$documentId"]}},
  {"$unset": ["language", "documentId"]}])      

Upvotes: 1

Related Questions