MongoDB Basic Operations

June 6th, 2011

MongoDB
This is part of MongoDB tutorial series, for this section we’ll learn about MongoDB basic operations in shell.

MongoDB is document oriented database, so rather than row in table we have document in collection.

Here a quick references if you want to play around with MongoDB shell.

  • Displays all the databases on the server you are connected to

    > show dbs
  • Creating new database or switching database

    > use demo
  • Inserting and saving document

    > post = {
    ...     'title' : 'learning mongodb',
    ...     'content' : 'mongodb is a document oriented database'
    ... }
    {
    	"title" : "learning mongodb",
    	"content" : "mongodb is a document oriented database"
    }
    > db.blog.insert(post)
  • Query all document
    > db.blog.find()
  • Query one document
    > db.blog.findOne()
  • Query documents based on specific value
    > db.blog.find({'title':'learning mongodb'})

    or

    > db.blog.find({'author':'gilang'})
  • Update document
    > post.author = 'gilang'
    gilang
    > db.blog.update({'title':'learning mongodb'}, post)

    Please note that post variable contain full data from earlier statement.

  • Update document with partial data
    > db.blog.update({'title':'learning mongodb'}, {'$set':{'pusblished_date': new Date()}})
  • Delete a document
    > db.blog.remove({'author':'gilang'})
  • Delete documents
    > db.blog.remove()
  • Drop collection
    > db.blog.drop()

Related posts:

  1. My First Step Into MongoDB – Installing MongoDB on Ubuntu
  2. Beginner Step Into MongoDB
  3. CodeIgniter Tutorial – To do list application part 3
  4. [Link] MongoEngine
  5. Full text search introduction

Tags:

2 Comments