Basic queries in MongoDB

Posted By : Amarjeet Yadav | 22-Nov-2020

Java mongodb

Loading...

In this blog, We are going to share some basic queries for mongo DB.

Creating an empty database and adding an authenticated User to this database:

mongo

>use testdb

//opens the database with the given name

//and sets this database as current DB.

//if this DB does not exist it will create an empty DB automatically.

>db.addUser("userName", "password");

//this user can only access the current database i.e. testdb 

//Now exit from the mongoDB:

>exit;

Now enter the mongoDB as authenticated user:

mongo -u username -p password 127.0.0.1/myDB       
           
//it open DB at given IP Adress with username and password

> j = { name : "Amarjeet" , salary : 2000 , age : 27};

> db.testCollection.save(j);

//creates a collection testCollection and insert a document in it.
//this collection is inserted in the current DB.

Note:
collection in MongoDB is the same as the table in MySQL.
document in MongoDB is the same as the row in MySQL.
When you apply the save method to a collection for the first time a new collection of that name is created and the document is inserted into it.

In MongoDB, there is not a predefinition of fields (what would be columns in an RDBMS). There are no schema for fields within documents the fields and their value datatype can vary.
MongoDB collections are essentially named groupings of documents.
MongoDB is a schema-free database.

Upon being inserted into the database, objects are assigned into objectID (if they do not already have one) in the field _id.

Saving new entry in "testCollection" collection

> db.testCollection.save({"name" : "Amarjeet", "salary" : 2000, "age" : 22 });

> db.testCollection.save({"name" : "saurabh", "salary" : 3000, "age" : 23, "mobile" : 911888812});

> db.testCollection.save({"age" : 20, "salary" : 500, "name" : "mukesh"});

> db.testCollection.save({"age" : 20, "salary" : 600, "name" : "gautam"});

Document Object Id:

ObjectId is small, likely unique, fast to generate, and ordered keys. ObjectId values are 12 bytes in length.

Retrieve data from “testCollection” collection

You can retrieve data from MongoDB using either of the following methods:

find()
findOne()

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.


>db.testCollection.find();

//prints all documents of a collection

Example 1: The following operation returns all documents in “testCollection” having name=Amarjeet.

> db.testCollection.find( {name:Amarjeet} );

In Mongo DB have some Operator
Comparison Operator

$all

$gt

$gte

$in

$lt

$lte

$ne

Logical Operator

$and

$nor

$not

$or

We provide end-to-end ERP development and integration services to enhance enterprise productivity and operational efficiency. Our team uses open-source tools and ERP platforms to custom build feature-rich enterprise applications from the ground up to address varied business needs. For more information, contact us at [email protected].