Select query on collection(table) in Mongodb

Select query on collection(table) in Mongodb

Documents(rows) associate keys with values

{
field:value
}

A collection(table) is a group of related documents(rows) that have a set of shared common indexes

db.employee.find()   — select * from employees

select name from employees

> db.employees.find({},{name:1})  — sec
{ “_id” : ObjectId(“540ebf2ffbf4b1a1447addec”), “name” : “abc123” }
{ “_id” : ObjectId(“540ebf3ffbf4b1a1447added”), “name” : “qwe” }
{ “_id” : ObjectId(“540ebf47fbf4b1a1447addee”), “name” : “zxc” }
{ “_id” : ObjectId(“540ebf52fbf4b1a1447addef”), “name” : “asd” }
{ “_id” : ObjectId(“540ebf5ffbf4b1a1447addf0”), “name” : “kkk” }

select age from employees

> db.employees.find({},{age:1})
{ “_id” : ObjectId(“540ebf2ffbf4b1a1447addec”), “age” : 55 }
{ “_id” : ObjectId(“540ebf3ffbf4b1a1447added”), “age” : 20 }
{ “_id” : ObjectId(“540ebf47fbf4b1a1447addee”), “age” : 25 }
{ “_id” : ObjectId(“540ebf52fbf4b1a1447addef”), “age” : 35 }
{ “_id” : ObjectId(“540ebf5ffbf4b1a1447addf0”), “age” : 23 }

select name,status from employees

you can also use alternative method to disable age column 0

> db.employees.find({},{age:0,_id:0})
{ “name” : “abc123”, “status” : “A” }
{ “name” : “qwe”, “status” : “A” }
{ “name” : “zxc”, “status” : “A” }
{ “name” : “asd”, “status” : “A” }
{ “name” : “kkk”, “status” : “N” }

if you do age:1 enable age

> db.employees.find({},{age:1,_id:0})
{ “age” : 55 }
{ “age” : 20 }
{ “age” : 25 }
{ “age” : 35 }
{ “age” : 23 }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.