AmitDiwan has Published 10744 Articles

Searching a specific domain name from URL records in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:45:07

298 Views

To search for a specific domain name, use /i. Let us create a collection with documents −> db.demo219.insertOne({"details":{"WebsiteURL":"www.EXAMPLE.com"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e667203d395bdc2134718") } > db.demo219.insertOne({"details":{"WebsiteURL":"www.gmail.com"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e667803d395bdc2134719") } > db.demo219.insertOne({"details":{"WebsiteURL":"www.example.com"}}); {    "acknowledged" : true,    "insertedId" : ... Read More

Using aggregation pipeline to fetch records in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:43:02

198 Views

The MongoDB aggregation pipeline has stages. Each stage transforms the documents as they pass through the pipeline.Let us first create a collection with documents −> db.demo218.insertOne({"Name":"Chris", "Branch":"CS", Marks:[65, 78, 36, 90]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e5f4903d395bdc2134712") } > db.demo218.insertOne({"Name":"David", "Branch":"ME", Marks:[56, 45, 42, 51]}); {   ... Read More

How to trim spaces from field in MongoDB query?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:40:10

2K+ Views

To trim spaces from field, use $trim in MongoDB. Let us create a collection with documents −> db.demo217.insertOne({"FullName":"   Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e5d1e03d395bdc213470f") } > db.demo217.insertOne({"FullName":"   David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e5d2503d395bdc2134710") } > db.demo217.insertOne({"FullName":"   John ... Read More

Projection of one column in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:39:06

196 Views

Let us first create a collection with documents −> db.demo216.insertOne({"ClientName":"John", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e351003d395bdc213470c") } > db.demo216.insertOne({"ClientName":"Bob", "ClientAge":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e351703d395bdc213470d") } > db.demo216.insertOne({"ClientName":"Mike", "ClientAge":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e351c03d395bdc213470e") }Display all documents from ... Read More

How to insert a boolean field in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:37:50

2K+ Views

Since boolean has two values: true and false, therefore, use true or false keyword in MongoDB. Let us create a collection with documents −> db.demo215.insertOne({"EmployeeDetails":[{EmployeeName:"David", "isMarried":false, "Salary":56000}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e344003d395bdc2134708") } > db.demo215.insertOne({"EmployeeDetails":[{EmployeeName:"Bob", "isMarried":true, "Salary":60000}]}); {    "acknowledged" : true,    "insertedId" : ... Read More

How to calculate average of a particular field in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:35:52

3K+ Views

To calculate average of a particular field, use aggregate() along with $avg. Let us create a collection with documents −> db.demo214.insertOne({"Marks":56, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e319403d395bdc2134705") } > db.demo214.insertOne({"Marks":86, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e319c03d395bdc2134706") } > db.demo214.insertOne({"Marks":78, "Name":"Carol"}); {   ... Read More

Aggregate multiple arrays into one huge array with MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:23:58

368 Views

To aggregate multiple arrays into a single array, use $project in MongoDB. Let us create a collection with documents −> db.demo119.insertOne( ...    { ...       "_id": 101, ...       "WebDetails": [ ...          { ...             "ImagePath": ... Read More

Using $addFields pipeline and run with the MongoDB $filter operator

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:19:04

969 Views

Let us first create a collection with documents −> db.demo118.insertOne( ...    { ...       "Id" : "101", ...       "Name" : "Chris", ...       "Subjects" : [ ...          "MySQL", ...          "MongoDB", ...       ... Read More

MongoDB Aggregate sum of values in a list of dictionaries for all documents?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:15:04

378 Views

To aggregate sum of values in a list of dictionaries, use $sum along with aggregate(). Let us create a collection with documents −> db.demo117.insertOne( ...    { ...       ID:101, ...       Details:[ ...          { ...             ... Read More

How to find only a single document satisfying the criteria in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 12:10:41

112 Views

To find only a single document, use findOne() in MongoDB. Let us create a collection with documents −> db.demo116.insertOne({"EmployeeId":101, "EmployeeName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2eff98d8f64a552dae635b") } > db.demo116.insertOne({"EmployeeId":102, "EmployeeName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2eff9fd8f64a552dae635c") } > db.demo116.insertOne({"EmployeeId":103, "EmployeeName":"David"}); {    "acknowledged" : ... Read More

Advertisements