In Mongodb, how do I add a field to every record?
I want to add the field "bio" to "about" sec开发者_Python百科tion of the document.
I suppose that you're using mongo console. To add a field to each document in a collection you have to use this command:
db.foo.update({},{$set : { "about.bio" : ""}} , true, true);
Of course you have to replace foo
with your real collection name. This command use empty string (""
) as default value for the new field: you can change this behaviour changing the value in the command.
Hope this helps
Adding a field to each document in a collection in cmd:
cd C:\Program Files\MongoDB\Server\4.2\bin
mongo
use yourdb
db.yourcollection.update({}, {$set:{"new_field":"value"}}, false, true)
This example is made for MongoDB 4.2
精彩评论