Adding and Querying the data in MongoDB - GeeksforGeeks

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 CollectionSyntax:
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:
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 fieldHere 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 fielddb.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 _idInserting Multiple Documents without _idExample 2: Inserting Multiple Documents with _idInserting Multiple Documents with _id
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 fieldHere 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
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
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 _idInserting Multiple Documents without _idExample 2: Inserting Multiple Documents with _idInserting Multiple Documents with _id
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> }, ... })

‘in’ Operator ($in) – The following example retrieve all documents which status value is “C” or “B”.Specify Conditions Using ‘in’ Operator.‘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 ‘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
‘in’ Operator ($in) – The following example retrieve all documents which status value is “C” or “B”.Specify Conditions Using ‘in’ Operator.
Specify Conditions Using ‘in’ Operator.
‘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 
The following example returns the documents which status is “A” and quantity is less than 540.
‘AND’ Operator 
‘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
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
MongoDB-operators
Picked
MongoDB
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
MongoDB - Distinct() Method
How to connect MongoDB with ReactJS ?
MongoDB - limit() Method
MongoDB - FindOne() Method
MongoDB insertMany() Method - db.Collection.insertMany()
Express.js express.Router() Function
How to set input type date in dd-mm-yyyy format using HTML ?
Installation of Node.js on Linux
Differences between Functional Components and Class Components in React
How to create footer to stay at the bottom of a Web page?