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
    • 623
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: