Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Basic MongoDB Queries

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 672
    Comment on it

    1) Create a "testDB" database.

    1. use testDB

    2) Create a collection name "users" and adding the value together using single command.
     

    1. db.logins.insert({username:"neetu"})

    or

     First create a collection of name logins
     

    1. db.createCollection("logins")

    Then add the users in collection.
     

    1. db.logins.insert({username:"neetubisht"})
    2. 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.
     

    1. db.logins.find()

    4) To get  users count in logins collection.
     

    1. db.logins.count()

    5) Find query with conditions.
     

    1. db.logins.find({username : "gunjan"})

    6) Find query with limited number of records.
     

    1. db.logins.find().limit(2)


    7) Display all the collections list
     

    1. show collections

     

    8) Like query syntax. It will display all the users having neetu in usename.
     

    1. db.users.find({"username": /neetu/})

    9) To get users list sorted by username in descending order

    1. db.mycollection.find().sort({"username":-1})

    10) To get users list sorted by username in ascending order.
     

    1. db.mycollection.find().sort({"username":1})

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: