1) Create a "testDB" database.
use testDB
2) Create a collection name "users" and adding the value together using single command.
db.logins.insert({username:"neetu"})
or
First create a collection of name logins
db.createCollection("logins")
Then add the users in collection.
db.logins.insert({username:"neetubisht"})
db.logins.insert({username:"gunjan"})
3) Use following command for fetching the data of collection . It will display all the users list of logins collection.
db.logins.find()
4) To get users count in logins collection.
db.logins.count()
5) Find query with conditions.
db.logins.find({username : "gunjan"})
6) Find query with limited number of records.
db.logins.find().limit(2)
7) Display all the collections list
show collections
8) Like query syntax. It will display all the users having neetu in usename.
db.users.find({"username": /neetu/})
9) To get users list sorted by username in descending order
db.mycollection.find().sort({"username":-1})
10) To get users list sorted by username in ascending order.
db.mycollection.find().sort({"username":1})
0 Comment(s)