Starting the shell
After having a working MongoDB installation, Make sure you have a running mongod instance and then run: mongo
Inserts and queries
Let us start by switching to the tutorial database:
use tutorial
You will see a message verifying that you have switched databases.
Note that creating the database is not required. Databases and collections are created only when documents are first inserted.
Note that creating the database is not required. Databases and collections are created only when documents are first inserted.
Now, we want to create our first document which is specified in JSON. For example:
{username: "amir"}
To save this document, you need to choose a collection to save it to. We will save it to the users collection.
db.users.insert({username: "amir"})
You can issue a simple query to verify that the document has been saved:
db.users.find()
Note that an _id field has been added to the document. You can think of the _id value as the document's primary key. Every MongoDB document requires an _id, and if one is not present when the document is created, then a special MongoDB object ID will be generated and added to the document at that time.
Let us add a second user to the collection:
db.users.save({username: "Michael"})
We can issue following to see how many documents are in a collection:
db.users.count()
A query selector is a document that is used to match against all documents in the collection. To query for all documents where the username is amir, you pass a simple document that acts as your query selector:
db.users.find({username: "amir"})
Updating documents
All updates require at least two arguments. The first specifies which documents to update, and the second defines how the selected documents should be modified.
Suppose that user "amir" decides to add his country of residence.
db.users.update({username: "amir"}, {$set: {country: "UK"}})
If you now issue a query, you will see that the document has been updated accordingly:
db.users.find({username: "amir"})
If the user later decides that he no longer wants his country stored in his profile, he can remove the value just as easily using the $unset operator:
db.users.update({username: "amir"}, {$unset: {country: 1}})
We can also have more complicated examples with more complex data structures:
{
username: "amir",
favourites: {
cities: ["Edinburgh", "London"],
sports: ["Squash", "Table tennis"]
}
}
We can update amir document to hold above example:
db.users.update( {username: "amir"},
{
$set: {
favorites: {
cities: ["Edinburgh", "London"],
movies: ["Squash", "Table tennis"]
}
}
}
)
Suppose that you want to find all users who like the city London:
db.users.find({"favourites.movies": "London"})
Now imagine you want to add another favourite city to all users who likes "London":
db.users.update( {"favourites.cities": "London"},
{$addToSet: {"favourites.cities": "Bristol"} },
false,
true )
Although we could use the operator $set like before, but that would require you to rewrite and send the entire array of movies. Instead we used the operator $addToSet. The fourth argument, true, indicates that this is a multi-update. By default, a MongoDB update operation will apply only to the first document matched by the query selector. If you want the operation to apply to all documents matched, then you must be explicit about that.
Deleting Data
If given no parameters, a remove operation will clear a collection of all its documents.
db.users.remove()
If you want to remove a certain subset of a collection’s documents, then you can pass a query selector to the remove() method. For example if we want to remove all users whose favourite city is Brighton:
db.users.remove({"favourites.cities": "Brighton"})
Note that the remove() operation does not actually delete the collection; it merely removes documents from a collection.
If your intent is to delete the collection along with all of its indexes, use the drop() method:
If your intent is to delete the collection along with all of its indexes, use the drop() method:
db.users.drop()