CRUD using the shell
The mongo shell is equivalent to the administration console used by relational databases. Connecting to the mongo shell is as easy as typing the following:
$ mongo
Type it on the command line for standalone servers or replica sets. Inside the shell, we can view available databases simply by typing the following:
$ db
And connect to a database by typing the following:
> use <database_name>
The mongo shell can be used for querying and updating data in our databases. Finding documents from a collection named books
is as easy as the following:
> db.books.find() { "_id" : ObjectId("592033f6141daf984112d07c"), "title" : "mastering mongoDB", "isbn" : "101" }
And inserting this document in the books
collection can be done via the following:
> db.books.insert({title: 'mastering mongoDB', isbn: '101'}) WriteResult({ "nInserted" : 1 })
The result we get back from MongoDB informs us that the write succeeded and inserted one new document in the database.
Deleting this document...