开发者

In Mongodb, how do I add a field to every record?

开发者 https://www.devze.com 2023-03-16 16:48 出处:网络
I want to add the field \"bio\" to \"about\" sec开发者_Python百科tion of the document.I suppose that you\'re using mongo console.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消