Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Database Articles
Page 242 of 546
Searching a specific domain name from URL records in MongoDB?
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" : ObjectId("5e3e667f03d395bdc213471a") }Display all documents from a collection with the help of find() method −> db.demo219.find();This will produce the following output −{ "_id" : ObjectId("5e3e667203d395bdc2134718"), "details" : { "WebsiteURL" : "www.EXAMPLE.com" } } { "_id" : ObjectId("5e3e667803d395bdc2134719"), "details" : { "WebsiteURL" : "www.gmail.com" } } { "_id" : ObjectId("5e3e667f03d395bdc213471a"), "details" : ...
Read MoreUsing aggregation pipeline to fetch records in MongoDB
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]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5f6203d395bdc2134713") } > db.demo218.insertOne({"Name":"Chris", "Branch":"CS", Marks:[78, 65, 89]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5f6c03d395bdc2134714") }Display all documents from a collection with the help of find() method −> db.demo218.find();This will produce the following output −{ "_id" : ObjectId("5e3e5f4903d395bdc2134712"), "Name" : "Chris", "Branch" ...
Read MoreHow to trim spaces from field in MongoDB query?
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 Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5d2b03d395bdc2134711") }Display all documents from a collection with the help of find() method −> db.demo217.find();This will produce the following output −{ "_id" : ObjectId("5e3e5d1e03d395bdc213470f"), "FullName" : " Chris Brown" } { "_id" : ObjectId("5e3e5d2503d395bdc2134710"), "FullName" : " David Miller" ...
Read MoreProjection of one column in MongoDB?
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 a collection with the help of find() method −> db.demo216.find();This will produce the following output −{ "_id" : ObjectId("5e3e351003d395bdc213470c"), "ClientName" : "John", "ClientAge" : 34 } { "_id" : ObjectId("5e3e351703d395bdc213470d"), "ClientName" : "Bob", "ClientAge" : 32 } { "_id" : ObjectId("5e3e351c03d395bdc213470e"), "ClientName" : "Mike", "ClientAge" : 35 }Following is the ...
Read MoreHow to insert a boolean field in MongoDB?
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" : ObjectId("5e3e344d03d395bdc2134709") } > db.demo215.insertOne({"EmployeeDetails":[{EmployeeName:"Chris", "isMarried":false, "Salary":78000}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e345a03d395bdc213470a") } > db.demo215.insertOne({"EmployeeDetails":[{EmployeeName:"Mike", "isMarried":true, "Salary":17000}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e346f03d395bdc213470b") }Display all documents from a collection with the help of find() method −> db.demo215.find();This will produce the following output −{ "_id" ...
Read MoreAggregate multiple arrays into one huge array with MongoDB?
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": "/all/image1", ... "isCorrect": "false" ... }, ... { ... "ImagePath": "/all/image2", ... "isCorrect": "true" ... } ... ...
Read MoreUsing $addFields pipeline and run with the MongoDB $filter operator
Let us first create a collection with documents −> db.demo118.insertOne( ... { ... "Id" : "101", ... "Name" : "Chris", ... "Subjects" : [ ... "MySQL", ... "MongoDB", ... "Java" ... ], ... "Details" : [ ... { ... "Name" : "David", ... S:"MongoDB" ... }, ... { ...
Read MoreMongoDB Aggregate sum of values in a list of dictionaries for all documents?
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:[ ... { ... Name:'Chris', ... Value1:20, ... Value2:30, ... Value3:10 ... }, ... { ... Name:'David', ... ...
Read MoreHow to find only a single document satisfying the criteria in MongoDB?
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" : true, "insertedId" : ObjectId("5e2effa5d8f64a552dae635d") } > db.demo116.insertOne({"EmployeeId":102, "EmployeeName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2effb7d8f64a552dae635e") }Display all documents from a collection with the help of find() method −> db.demo116.find();This will produce the following output −{ "_id" : ObjectId("5e2eff98d8f64a552dae635b"), "EmployeeId" : 101, "EmployeeName" : "John" } { "_id" ...
Read MoreFind maximum score for duplicate Name values in MongoDB?
To find maximum score, use GROUP() to group documents in a collection. Let us create a collection with documents −> db.demo114.insertOne({"Score":60, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc57d8f64a552dae6354") } > db.demo114.insertOne({"Score":87, "Nam+e":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc5ad8f64a552dae6355") } > db.demo114.insertOne({"Score":45, "Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc5dd8f64a552dae6356") } > db.demo114.insertOne({"Score":67, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc68d8f64a552dae6357") } > db.demo114.insertOne({"Score":38, "Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc74d8f64a552dae6358") }Display all documents from a collection with the help of find() method −> db.demo114.find();This will produce the ...
Read More