MongoDB Tutorial - MongoDB HelloWorld








In MongoDB, both database and table are created automatically when data is inserted for the first time.

We can use the use databaseName command to switch to your database even if the database itself is not created yet.

In the following example, after inserting a new record, database "java2s", and table "users" are created on the fly.

$ ./mongo
> use java2s
switched to db java2s
?
> db.users.insert({username:"java2s",password:"123456"})
> db.users.find()
{ "_id" : ObjectId("the id value"), "username" : "java2s", "password" : "123456" }

show dbs command lists all databases. use yourDBName command switches to db_name. show collections command lists all tables in the current selected database.





Insert

To insert a record, uses db.yourTableName.insert({data}) or db.tablename.save({data}).

> db.users.save({username:"abc",password:"abc123"})
> db.users.find()

To update a record, uses db.tablename.update({criteria},{$set: {new value}}). In below example, the password of username : "java2s" is updated.

> db.users.update({username:"java2s"},{$set:{password:"hello123"}})
> db.users.find()

Find

To find or query records, uses db.tablename.find({criteria}).

List all records:

> db.users.find()

Find records where username is "google"

> db.users.find({username:"abc"})

Find records where username's length is less than or equal to 2.

db.users.find({$where:"this.username.length<=2"})

Find records where username field is existed.

db.users.find({username:{$exists : true}})




Remove

To delete a record, uses db.tablename.remove({criteria}).

> db.users.remove({username:"google"})
> db.users.find()

Index

List all indexes of table "users", by default the column "_id" is always the primary key and created automatically.

> db.users.getIndexes()
>

To create an index, uses db.tablename.ensureIndex(column).

> db.users.ensureIndex({username:1})
> db.users.getIndexes()

To drop an index, uses db.tablename.dropIndex(column). In the following example, the index on column "username" is deleted.

> db.users.dropIndex({username:1})
> db.users.getIndexes()
>

To create an unique index, uses db.tablename.ensureIndex({column},{unique:true}). The following code creates an unique index on column "username".

> db.users.ensureIndex({username:1},{unique:true});
> db.users.getIndexes()

Help

List all available commands.

> help
  db.help()                    help on db methods
  db.mycoll.help()             help on collection methods
    ...

db.help() - Shows help on db.

> db.help()
DB methods:
  db.addUser(username, password[, readOnly=false])
  db.auth(username, password)
  ...

db.collection.help() - Shows help on collection (table).

> db.users.help()
DBCollection help
  db.users.find().help() - show DBCursor help
  db.users.count()
  db.users.dataSize()
  ...

db.collection.function.help() - Shows help on function.

> db.users.find().help()
find() modifiers
  .sort( {...} )
  .limit( n )
  .skip( n )
  .count()
  .size()
  .explain([verbose])
    ...