In this recipe, we will look at basic CRUD operations with MongoDB. We will learn how to create databases, store, retrieve, and update stored data. This is a recipe to get started with MongoDB.
Make sure that you have installed and configured MongoDB. You can also use the MongoDB installation on a remote server.
Follow these steps to store and retrieve data with MongoDB:
Open a shell to interact with the Mongo server:
$ mongo
To open a shell on a remote server, use the command given. Replace
server_ip
andport
with the respective values:$ mongo server_ip:port/db
To create and start using a new database, type
use dbname
. Since schemas in MongoDB are dynamic, you do not need to create a database before using it:> use testdb
You can type
help
in Mongo shell to get a list of available commands and help regarding a specific command:> help
: Let’s insert our first document:> db.users.insert({‘name’:’ubuntu’,’uid’:1001...