Mongodb’s article said:
http://docs.mongoing.com/manu …
MongoDB uses dynamic mode. You can create a set without predefined patterns. You can modify the structure of a document by adding or deleting fields directly within the document. Documents do not need to have the same set of fields within a set.
Question:
I want to add fields and modify field names. How do I do this?
Add field:“Season”
{ $set: { <field1>: <value1>, ... } }
Modify field name:“team”→”NBA Team”
{$rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
Mongo-shell complete sample code:
use nba db.createCollection(players) db.players.insert({"team":"Cleveland","firstname":"Irving","lastname":"Kyrie"}) db.players.update({"firstname":"Irving"},{$set:{"team":"Cletics","Season":"2017~2018"}}) db.players.update({"firstname":"Irving"},{$rename:{"team":"NBA Team"}})
Record data changes:
{ "_id" : ObjectId("59a1a734c8143c78793d3da6"), "firstname" : "Irving", "lastname" : "Kyrie", "team" : "Cleveland" } { "_id" : ObjectId("59a1a734c8143c78793d3da6"), "firstname" : "Irving", "lastname" : "Kyrie", "team" : "Cletics", "Season" : "2017~2018" } { "_id" : ObjectId("59a1a734c8143c78793d3da6"), "firstname" : "Irving", "lastname" : "Kyrie", "NBA Team" : "Cletics", "Season" : "2017~2018" }