Basic queries for CRUD operations in MongoDB II
Posted By : Akash Sharma | 01-May-2013
In this blog I am going to share some more CRUD operations in mongoDB.
Previously I have written a blog for Basic queries for CRUD operations in MongoDB .
Let say we have a database “school” having collection “student”.
Example 1: How to rename a field in a collection.
The $rename operator updates the name of a field. The new field name must differ from the existing field name.
I have a field named “class” of a student.I want to rename it as “standard”.
> db.student.update( {} , {$rename:{"class" : "standard"}} ,false , true)
The first blank curly braces indicates that you have to update all the documents.
parameters false and true are for upsert and multi options respectively.
Example 2: How to get a formatted output of all the documents.
If I apply the command
>db.student.find()
I will get an output like this:
> db.student.find() { "_id" : ObjectId("517ce315ebdc79a0c6ad6948"), "age" : 15, "name" : "rahul", "s tandard" : 9, "subjects" : [ "hindi", "maths", "science" ] } { "_id" : ObjectId("517ce328ebdc79a0c6ad6949"), "age" : 17, "name" : "ashok", "s tandard" : 10, "subjects" : [ "hindi", "maths", "science" ] } { "_id" : ObjectId("517ce341ebdc79a0c6ad694a"), "age" : 14, "name" : "ankur", "s tandard" : 6, "subjects" : [ "hindi", "maths", "science" ] } >
To make a formated output we can apply this command:
> db.student.find().pretty() { "_id" : ObjectId("517ce315ebdc79a0c6ad6948"), "age" : 15, "name" : "rahul", "standard" : 9, "subjects" : [ "hindi", "maths", "science" ] } { "_id" : ObjectId("517ce328ebdc79a0c6ad6949"), "age" : 17, "name" : "ashok", "standard" : 10, "subjects" : [ "hindi", "maths", "science" ] } { "_id" : ObjectId("517ce341ebdc79a0c6ad694a"), "age" : 14, "name" : "ankur", "standard" : 6, "subjects" : [ "hindi", "maths", "science" ] } >
Example 3: How to get output for limited number of fields but not for all.
Let say I want to see only name, age and standard for all documents:
> db.student.find({},{name:1,standard:1,age:1}).pretty() { "_id" : ObjectId("517ce315ebdc79a0c6ad6948"), "age" : 15, "name" : "rahul", "standard" : 9 } { "_id" : ObjectId("517ce328ebdc79a0c6ad6949"), "age" : 17, "name" : "ashok", "standard" : 10 } { "_id" : ObjectId("517ce341ebdc79a0c6ad694a"), "age" : 14, "name" : "ankur", "standard" : 6 } > db.student.find({},{name:1,standard:1,age:1,_id:0}).pretty() { "age" : 15, "name" : "rahul", "standard" : 9 } { "age" : 17, "name" : "ashok", "standard" : 10 } { "age" : 14, "name" : "ankur", "standard" : 6 } >
The first curly braces in find() will filter the documents on some condition.
The second curly braces determines which fields you want to show.
If you want to see the value for a particular field you have to set its value as 1.
By default value for _id field is 1 and 0 for all other fields.
Example 4: How to get output for limited number of fields but not for all for a given condition.
I want to see only name, age and standard for documents having standard greater than 8:
> db.student.find({standard:{$gt:8}},{name:1,standard:1,age:1,_id:0}) { "age" : 15, "name" : "rahul", "standard" : 9 } { "age" : 17, "name" : "ashok", "standard" : 10 } >
Example 5 : How to remove a field from all documents of a collection.
suppose we want to remove age field from all documents:
> db.student.update({age:{$exists:true}},{$unset:{age:1}},false,true)
Here $exists checks in those documents which contains age field.
$unset removes the field.
false and true are for upsert and multi options respectively.
Akash Sharma
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Sharma
Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis