MongoDb Interactive Shell

  • Add new user

> db.addUser(‘akedar’,'akedar’)
{
“user” : “akedar”,
“readOnly” : false,
“pwd” : “fe3681e44bbe75a79faf7c48573c1ba6″
}

  • Show collections

> show collections
system.indexes
system.users

  • Show users

> show users

{
“_id” : ObjectId(“4c6574d6fb4d000000002a70″),
“user” : “akedar”,
“readOnly” : false,
“pwd” : “fe3681e44bbe75a79faf7c48573c1ba6″
}

  • Show databases

> show dbs
admin
local
test

  • Create First record. MongoDB will create the collection if it does not exist

> comment={author:”tom”, date:new Date()}
{
“author” : “tom”,
“date” : “Fri Aug 13 2010 13:29:09 GMT-0400 (Eastern Daylight Time)”
}
> db.comments.save( comment)
>
> show collections
comments
system.indexes
system.users
>

  • Create second record, with additional fields in the record.

> comment={author:”Tom”, date:new Date(), text:”MongoDB is cool”}
{
“author” : “Tom”,
“date” : “Fri Aug 13 2010 13:42:34 GMT-0400 (Eastern Daylight Time)”,
“text” : “MongoDB is cool”
}
> db.comments.save( comment)

  • Updating a record.

> rec1=db.comments.findOne({author:”Tom”})
{
“_id” : ObjectId(“4c6595a4c706000000004ead”),
“author” : “Tom”,
“date” : “Fri Aug 13 2010 14:57:10 GMT-0400 (Eastern Daylight Time)”,
“text” : “MongoDB is cool”
}
> rec1.author=”Tommy”;
Tommy
> db.comments.save( rec1)
> rec1=db.comments.count()
2
> rec1=db.comments.find()
{ “_id” : ObjectId(“4c6595a4c706000000004ead”), “author” : “Tommy”, “date” : “Fri Aug 13 2010 14:57:10 GMT-0400 (Eastern Daylight Time)”, “text” : “MongoDB is cool” }
{ “_id” : ObjectId(“4c65963b8140000000004e83″), “author” : “mark”, “date” : “Fri Aug 13 2010 15:00:08 GMT-0400 (Eastern Daylight Time)”, “text” : “MongoDB is not cool” }

  • Deleting a record.

> db.posts.count()
2
> db.posts.remove({author: “Tom”})
> db.posts.count()
1

  • Deleting all records

> db.posts.remove()
> db.posts.count()
0

  • Deleting a collection.

> db.posts.drop()
true
> show collections
system.indexes
system.users
>

MongoDB – Installation and First Steps

MongoDb Interactive shell – basic commands

MongoDb Interactive shell – searching records

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.