top of page

Mongo Insert, Update, Remove query | Projection in MongoDB | Sort, Limit and Relation

Updated: Jun 12, 2023


Start server and mongodb using mongodb shell after installation which we discuss how to install mongodb in previous blog.

1. Insert record:

Before running insert query find collection with one row and all attributes to understand it easily.

Run db.collectionname.findOne().

>db.inspections.findOne


Question- Insert the record in collection "inspections" where

"id":"10021-2015-ENFP",

"certificate_number":9278809,

"business_name" : "Codersarts", "date" : "Feb 20 2019",

"result" : "No Violation Issued",

"sector" : "Cigarette Retail Dealer - 127".


2. Remove(delete) record:

If we want to remove by record with all column attribute than run query accordingly.


Remove(delete) by given criteria(condition): Run given all query to see how it work according to criteria

> db.inspections.find({})

>criteria = {"result" : "No Violation Issued"}


>db.inspections.find(criteria)


>db.inspections.remove(criteria)


if you want to delete only one document of given criteria then use

>db.inspections.remove(criteria,1)

It delete only one record from top as per given

criteria(condition)

3. Update

Question- Update multiple column value in database where put "result"= "Violation Issued" at the place of "result"="Pass" in all database.

> db.inspections.find({})


4- Projection in MongoDB:

A projection query is a query where you specify which fields should be returned.

You can do this by including the field names in your query, and adding a 1 or 0 next to them, to specify whether it should be returned or not. This is a projection parameter.

A projection parameter of 1 will display the field and a 0 will hide it.

Now, let's use projection to display just the date,result and except id field field:

>db.inspections.find( { "result": "Violation Issued" }, { "_id":0,"date": 1 ,"result":1} )

Result:


You can't mix 1s(inclusion) and 0s(Exclusion) like:

>db.inspections.find( { "result": "Violation Issued" }, { "date": 1, "sector":0 } )

Result: show error:


5- Limit the Results of a Query:

Return only the number of documents you need with the limit() method.

In MongoDB, you can use the limit() method to specify a maximum number of documents for a cursor to return.

When you query a collection using the db.inspections.find() method, you can append limit() to specify the limit.

Ok, so let's limit the results to say, 3 documents:

>db.inspections.find( { "result": "Violation Issued" }, { "date": 1 } ).limit(3)


6- Skip() Method:

You can use the skip() method to skip to a document within the cursor.

Skip first result and display next three result


Note that skip() can be used on any query (not just ones with limit()).

7- Sort the Results of a Query:

The sort() method specifies a sort order for the cursor.

It will typically contain one or more fields, each followed by either 1 or -1, depending on whether the sort should be in ascending or descending order.

With sort() in Ascending Order:

A value of 1 next to a field name specifies that the documents should be sorted by the specified field in ascending order.

Here we sort the results by name in ascending order:

>db.inspections.find( ).limit(3).sort( { "result": 1} )


With sort() in Descending Order:

A value of -1 next to a field name specifies descending order.

Here we sort the results by name in descending order:

>db.inspections.find( ).limit(3).sort( { "result": -1} )


Multiple Fields

>db.inspections.find( ).sort( { "result": 1, "date": -1 } )

8- Create a Relationship:

To create a relationship in MongoDB, either embed a BSON document within another, or reference it from another.

One-to-One Relationship One-to-Many Relationship

Querying the Relationship:

you can use $lookup to perform a left outer join on the two collections.This, in conjunction with the aggregate() method, and $match.

Example:


>db.collection1.aggregate([

    {

      $lookup:

        {

          from: "collection2",

          localField: "_id",

          foreignField: "artist_id",

          as: "band_members"

        }

   },

   { $match : { artistname : "Rush" } }

]).pretty()

May I hope you this blog provide you some important query in MongoDB which help you to run easily on the project.

If you face any other issue regarding this please visit and contact here

bottom of page