In this blog I am going to share some basic quiries for mongoDB.
Creating a empty database and adding an authenticated User to that database:
Now enter the mongoDB as authenticated user:
collection in mongoDB is same as table in mysql.
document in mongoDB is same as row in mysql.
When you apply save method to a collection for the first time a new collection of that name is created and document is inserted into it.
In MongoDB there is no predefinition of fields (what would be columns in an RDBMS). There is no schema for fields within documents – the fields and their value datatypes can vary.
MongoDB collections are essentially named groupings of documents. You can think of them as roughly equivalent to relational database tables.
MongoDB is a schema-free (or more accurately, "dynamic schema") database.
Upon being inserted into the database, objects are assigned an object ID (if they do not already have one) in the field _id.
Saving new entry in "myCollection" collection
Document Object Id:
ObjectId: A special 12-byte BSON type that has a high probability an ObjectId represent the time of the ObjectID’s creation. MongoDB uses ObjectId values as the default values for _id fields.
_id : A field containing a unique ID, typically a BSON ObjectId. If not specified, this value is automatically assigned upon the creation of a new document. You can think of the _id as the document’s primary key.
Retrieve data from “myCollection” collection
You can retrieve documents from MongoDB using either of the following methods:
The find() method has the following syntax:
db.collection.find( <query>, <projection> )
· If there is no <query> argument, the find() method selects all documents from a collection.
· If there is a <query> argument, the find() method selects all documents from a collection that satisfies the criteria of the query.
Example 1: The following operation returns all documents in “myCollection” having name=sachin .
Example 2: find all entries where name starts with "r" and age =22.
Example 3: find all entries where name=rahul or name=sachin.
Example 4: find all entries where name contains character "s" or salary >=22.
Example 5: find name and age of all entries where name has a charater 'b'.
Example 6: find all names in collection in sorted order by name in ascending order.
The _id field is implicitly included in the <projection> argument. In projections that explicitly include fields, _id is the only field that you can explicitly exclude. Otherwise, you cannot mix include field and exclude field specifications.
Example 7: find all names in collection in sorted order by age in ascending order and if age is same sort it by name in descending order.
Example 8: find all names having salary greater than 500 in collection in sorted order by age in ascending order and if age is same sort it by name in descending order.
For more help:
How to Update a document :
The update() method modifies an existing document or documents in a collection. By default the update() method updates a single document. To update all documents in the collection that match the update query criteria, specify the multi option. To insert a document if no document matches the update query criteria, specify the upsert option.
db.collection.update(query, update, <upsert,> <multi>)
The update() method takes the following parameters:
query (document) – Specifies the selection criteria for the update. The query parameter employs the same query selectors as used in the db.collection.find() method.
update (document) – Specifies the modifications to apply.
If the update parameter contains any update operators expressions such as the $set operator expression, then:
· the update parameter must contain only update operators expressions.
· the update method updates only the corresponding fields in the document.
If the update parameter consists only of field: value expressions, then:
· the update method replaces the document with the updates document. If the updates document is missing the _id field, mongoDB will add the _id field and assign to it a unique objectId .
· the update method updates cannot update multiple documents.
options : (optional) Specifies whether to perform an upsert and/or a multiple update. Use the options parameter instead of the individual upsert and multi parameters.
upsert : A kind of update that either updates the first document matched in the provided query selector or, if no document matches, inserts a new document having the fields implied by the query selector and the update operation. The default value is false. When true, the update() method will update an existing document that matches the query selection criteria or if no document matches the criteria, insert a new document with the fields and values of the update parameter and if the update included only update operators, the query parameter as well .
multi : (optional) Specifies whether to update multiple documents that meet the query criteria.
When not specified, the default value is false and the update() method updates a single document that meet the query criteria.
When true, the update() method updates all documents that meet the query criteria.
Example9 : change salary = 5000 in collection where name == ‘sachin’ .
Example 10: change salary=200 for all entries where age=20.
Example 11: Add new field “status” having value as “senior” if salary is greater than or equal to 1000.
Example 12: Add new field “profile” to all documents as “player”.
Example 13: Drop field “status” from all documents.
for more info on update go to:
Get number of documents in a collection:
How to delete documents from a collection:
How to drop entire collection:
How to delete database:
How to show all databases:
How to show all collections in current database:
Showing statistics of collection:
Showing statistics of database:
Here are some screenshots for the above commands:
Adding new DB and authenticated user
Saving new documents and retrieving data
For some advanced mongoDB quiries I have written a blog Basic queries for CRUD operations in MongoDB II.