Contents of page >
- 1) How to add new field in document in collection in MongoDb>
1) How to add new field in document in collection in MongoDb>
1.1) First, insert in collection in MongoDB.
> db.employee.insert({id : 1, firstName:"ankit"})
|
Above line will create collection(table) (if collection already exists it will insert records in it).
1.2) Now, let’s query employee collection in MongoDb >
> db.employee.find()
>{ "_id" : ObjectId("5869200ee26361c436d65ee9"), "id" : 1, "firstName" : "ankit" }
|
2.1) Now, let’s add salary field in document where id = 1>
> db.employee.update({id:1},{$set:{salary:1000}})
|
2.2) Now, let’s again query employee collection in MongoDb >
> db.employee.find()
{ "_id" : ObjectId("5869200ee26361c436d65ee9"), "id" : 1, "firstName" : "ankit", "salary" : 1000 }
|
Now, we can see that salary field was added in document where id = 1.
Summary -
So in this core java tutorial we learned how to add new field in document in collection in MongoDb.
Example - This adds salary field in document where id = 1
> db.employee.update({id:1},{$set:{salary:1000}})
|
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.
RELATED LINKS>
Using like statement (as in sql) in MongoDB
Update documents in collection in mongoDB
Delete documents in collection (table) in mongoDB
How to Delete all documents from collection in MongoDB
Labels:
MongoDB