top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

MongoDB : Is mongoDB support to add a new (key:value) pair for an existing document ?

+1 vote
686 views

Query description: I have a collection name student_db and added a document in a collection with entries (name: "alok, age : 31 and profession: "xyz"). After inserting the document into student_db database, I want to add another (key:value ) pair i.e. salary: 50000.

Can someone help me out to resolve this problem ?

posted Jul 18, 2015 by Alok

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

yes it supports,If you want to add more {"key":"value"} pair in your collection of student_db,use update method and set the criteria which will specify the document id. This update command will not find any data to replace and It will add given {"key":"value"} pair to the document of specified collection.
Here is script Sample:
->use student_db;
->db.SampleCollectionName.update({"_id":ObjectId("DocumentID")},{$set:{"salary":50000}});

answer Jan 29, 2016 by Shivam Kumar Pandey
...