You must follow this. Lets Start with example and we will add the new field in the document.
Insert Some Document in
student collection where further we can apply the MongoDB Collection Methods to
add new field in existing document in student Collection. Inserted document in student Collection. Below is the document list present in student collection.
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
"id" : 1,
"name" : "M Kumar",
"dob" : "2003-04-07",
"marks" : 54,
"created_at" : "2017-11-13 23:33:42"
}
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
"id" : 2,
"name" : "MOHAMMED ISHAQ H",
"dob" : "2002-06-14",
"marks" : 67,
"created_at" : "2016-11-13 23:33:42"
}
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
"id" : 3,
"name" : "ANBUSELVAM S",
"dob" : "2004-06-18",
"marks" : 87,
"created_at" : "2019-11-13 23:33:42"
}
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
"id" : 4,
"name" : "MARTINPRIYADOSS J",
"dob" : "2004-03-08",
"marks" : 43,
"created_at" : "2012-11-13 23:33:42"
}
{
"_id" : ObjectId("5ebf58376803d36568082c60"),
"id" : 5,
"name" : "JAGAN J",
"dob" : "2003-04-10",
"marks" : 98,
"created_at" : "2012-11-13 23:33:42"
}
Add New fields in existing Document in Collection
Syntax for Update method in MongoDB is below. By using the below customize code we will
add new field in document.
db.collection.update( criteria, objNew, upsert, multi )
Criteria : This will hold the criteria for updating the document. Here you can specify the condition whether you want to update all the document or few document in collection.
objNew : $operator which will manipulate the object.
upsert : It is Upsert Operation which will insert the record if it doesn't exist.
multi : Indicates matched documents criteria should be updated or just one Document.
Adding new field in the student Collection
Now we will
Add new Field named
status in
student Collection document and set the value to 1. Below is the code for your reference.
db.student.update({}, {$set :{"status": 1}}, false, true)
Above command will
add the new field in existing document of Student Collection. Run the command and again fetch the document using find() method you will see that all the document got updated with status filed.
db.student.find().pretty()
All the document have the new field named with status and value is assigned to 1.
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
"id" : 1,
"name" : "M Kumar",
"dob" : "2003-04-07",
"marks" : 54,
"created_at" : "2017-11-13 23:33:42",
"isOpen" : false,
"status" : 1
}
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
"id" : 2,
"name" : "MOHAMMED ISHAQ H",
"dob" : "2002-06-14",
"marks" : 67,
"created_at" : "2016-11-13 23:33:42",
"isOpen" : false,
"status" : 1
}
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
"id" : 3,
"name" : "ANBUSELVAM S",
"dob" : "2004-06-18",
"marks" : 87,
"created_at" : "2019-11-13 23:33:42",
"isOpen" : false,
"status" : 1
}
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
"id" : 4,
"name" : "MARTINPRIYADOSS J",
"dob" : "2004-03-08",
"marks" : 43,
"created_at" : "2012-11-13 23:33:42",
"isOpen" : false,
"status" : 1
}
{
"_id" : ObjectId("5ebf58376803d36568082c60"),
"id" : 5,
"name" : "JAGAN J",
"dob" : "2003-04-10",
"marks" : 98,
"created_at" : "2012-11-13 23:33:42",
"isOpen" : false,
"status" : 1
}
Add new Filed exam_status as Pass in Document if marks > 60
Query to add new field exam_status as pass if the marks greater then 60. Below is the code which will add new fields in
student collection.
db.student.update({marks : {$gt :60}}, {$set :{"exam_status" : "PASSED"}}, false, true)
this above query will add the
exam_status field with value pass if the marks is greater then 60. check the below code and the output.
db.student.find().limit(3).pretty()
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
"id" : 1,
"name" : "M Kumar",
"dob" : "2003-04-07",
"marks" : 54,
"created_at" : "2017-11-13 23:33:42",
"isOpen" : false,
"status" : 1
}
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
"id" : 2,
"name" : "MOHAMMED ISHAQ H",
"dob" : "2002-06-14",
"marks" : 67,
"created_at" : "2016-11-13 23:33:42",
"isOpen" : false,
"status" : 1,
"exam_status" : "PASSED"
}
{
"_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
"id" : 3,
"name" : "ANBUSELVAM S",
"dob" : "2004-06-18",
"marks" : 87,
"created_at" : "2019-11-13 23:33:42",
"isOpen" : false,
"status" : 1,
"exam_status" : "PASSED"
}