Related Articles

Related Articles

Adding and Querying the data in MongoDB
  • Last Updated : 09 Sep, 2020

Adding Data in MongoDB: MongoDB stores document in BSON, a binary form of JSON (JavaScript Object Notation). The documents are store in collection.

To insert documents in MongoDB follow the step mentioned below:

Step 1: Create Collection
Syntax:

use collection_name

It will create a collection if it does not exist otherwise it returns the existing collection.

Create a collection

To show the currently selected collection use ‘db’ command as shown below:

Currently selected collection 



Step 2: Insert Data into Collection Documents in MongoDB can be inserted using two methods:

  1. db.collection_name.insertOne(): The db.collection_name.insertOne method is used to insert single document in collection.

    Syntax:

    db.collection_name.insertOne(item:'item1'}) // OR
    db.collection_name.insert(item:'item1'})
    

    Example:

    Inserting a document  without _id field

    Here we can specify the _id field if it does not specify then MongoDB adds the _id field with value objectId.

    Inserting a document with _id field

  2. db.collection_name.insertMany(): The db.collection_name.insertMany() can insert multiple documents into a collection.Only you need to pass array of documents to this method.

    Syntax:

    db.collection_name.insertMany([{item:'item1'}, {item:'item2'}, 
    {item:'item3'}, {item:'item4'}]) 
    

    Example 1: Inserting Multiple Documents without _id

    Inserting Multiple Documents without _id

    Example 2: Inserting Multiple Documents with _id

    Inserting Multiple Documents with _id

Querying the data in MongoDB: The query operation performs using db.collection.find() method in MongoDB. To query documents in MongoDB follow the steps mentioned below:



Step 1: Create collection and Insert documents using Mongo Shell

Insert Documents in Collection

Step 2: Select All Documents in a Collection – To select all documents in the collection, pass an empty document as the query filter parameter to the find method. This statement is similar to the SELECT * FROM table statement in MySQL.

Syntax:

db.collection_name.find({})

Select All Documents in a Collection

Step 3: Specify Equality Condition – To filter the result of db.collection_name.find() method you need to specify conditions for method.

Syntax:

db.collection_name.find({ <field1>: <value1>, ... })

Specify Equality Condition

Step 4: Specify Conditions Using Query Operators – A query filter document can use the query operators to specify a condition.

Syntax:

db.collection_name.find({ <field1>: { <operator1>: <value1> }, ... })
  1. ‘in’ Operator ($in) – The following example retrieve all documents which status value is “C” or “B”.

    Specify Conditions Using ‘in’ Operator.

  2. ‘AND’ Operator(, ) – A compound query can specify conditions for more than one field in the collection’s documents. Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.

    The following example returns the documents which status is “A” and quantity is less than 540.

    ‘AND’ Operator 

  3. ‘OR’ Operator ($or) – Using the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition.

    The following example returns the documents which status is “A”  or quantity is less than 540.

    ‘OR’ Operator ($or)

    You can use ‘AND’ and ‘OR’  operators at a same time, in the following example the query returns documents which status equals “A” and either qty is less than 540 or item starts with the character “i”.

    Using ‘AND’ and ‘OR’  same time

My Personal Notes arrow_drop_up
Recommended Articles
Page :