Found 1659 Articles for Big Data Analytics

Removing item from array in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 12:55:27

98 Views

To remove item from array, use $pull in MongoDB. Let us create a collection with documents −> db.demo224.insertOne({"ListOfTechnology":["Spring", "Hibernate", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee6d103d395bdc2134733") } > db.demo224.insertOne({"ListOfTechnology":["Groovy"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee6ec03d395bdc2134734") }Display all documents from a collection with the help of find() method −> db.demo224.find();This will produce the following output −{ "_id" : ObjectId("5e3ee6d103d395bdc2134733"), "ListOfTechnology" : [ "Spring", "Hibernate", "Java" ] } { "_id" : ObjectId("5e3ee6ec03d395bdc2134734"), "ListOfTechnology" : [ "Groovy" ] }Following is the query to remove item from array in MongoDB −>db.demo224.update({_id:ObjectId("5e3ee6d103d395bdc2134733")}, {$pull:{"ListOfTechnology":"Java"}}); WriteResult({ "nMatched" : 1, "nUpserted" : ... Read More

How to compare multiple properties in MongoDB?

AmitDiwan
Updated on 30-Mar-2020 12:52:45

240 Views

To compare multiple properties, use $where in MongoDB. Let us create a collection with documents −> db.demo223.insertOne({"Scores":[56, 78]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee4ca03d395bdc2134730") } > db.demo223.insertOne({"Scores":[88, 45]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee4d103d395bdc2134731") } > db.demo223.insertOne({"Scores":[98, 79]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee4d803d395bdc2134732") }Display all documents from a collection with the help of find() method −> db.demo223.find();This will produce the following output −{ "_id" : ObjectId("5e3ee4ca03d395bdc2134730"), "Scores" : [ 56, 78 ] } { "_id" : ObjectId("5e3ee4d103d395bdc2134731"), "Scores" : [ 88, 45 ] } { "_id" : ObjectId("5e3ee4d803d395bdc2134732"), "Scores" : ... Read More

MongoDB query to add new array element in document

AmitDiwan
Updated on 30-Mar-2020 12:50:24

415 Views

To add new array element in a MongoDB document, use $(projection) along with update(). Let us create a collection with documents −>db.demo222.insertOne({"details":[{"StudentName":"Chris", "StudentMarks":78}, {"StudentName":"David", "StudentMarks":89}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee31703d395bdc213472f") }Display all documents from a collection with the help of find() method −> db.demo222.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e3ee31703d395bdc213472f"),    "details" : [       {          "StudentName" : "Chris",          "StudentMarks" : 78       },       {          "StudentName" : "David",          "StudentMarks" ... Read More

MongoDB regular expression to fetch record with specific name “John”, instead of “john”

AmitDiwan
Updated on 30-Mar-2020 12:48:48

62 Views

For searching a specific word, use /searchWord/ with regex. Let us create a collection with documents −> db.demo221.insertOne({"Details":{"StudentName":"Chris", "StudentAge":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee15d03d395bdc213472b") } > db.demo221.insertOne({"Details":{"StudentName":"John", "StudentAge":20}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee16503d395bdc213472c") } > db.demo221.insertOne({"Details":{"StudentName":"Bob", "StudentAge":22}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee16b03d395bdc213472d") } > db.demo221.insertOne({"Details":{"StudentName":"john", "StudentAge":24}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ee17303d395bdc213472e") }Display all documents from a collection with the help of find() method −> db.demo221.find();This will produce the following output −{ "_id" : ObjectId("5e3ee15d03d395bdc213472b"), "Details" : { "StudentName" : "Chris", "StudentAge" : 21 } ... Read More

Get all embedded documents with “isMarried” status in a MongoDB collection

AmitDiwan
Updated on 30-Mar-2020 12:47:39

149 Views

To get all embedded documents, use $project in MongoDB. Let us create a collection with documents −> db.demo220.insertOne({ ...   "id":101, ...   "FullName" : "John Doe", ...   "EmailId" : "john12@gmail.com", ...   "ShippingDate" : new ISODate(), ...   "details" : { "_id" :1001, "isMarried" :true } ...} ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3eaf5b03d395bdc213471d") } > db.demo220.insertOne( ...{ ...   "id":102, ...   "FullName" : "John Smith", ...   "EmailId" : "johnsmith@gmail.com", ...   "ShippingDate" : new ISODate(), ...   "details" : { "_id" :1002, "isMarried" :false } ... } ... ); {    "acknowledged" ... Read More

Searching a specific domain name from URL records in MongoDB?

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

183 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" : 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 More

Using aggregation pipeline to fetch records in MongoDB

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

134 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]}); {    "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 More

How to trim spaces from field in MongoDB query?

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

1K+ 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 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 More

Projection of one column in MongoDB?

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

121 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 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 More

How to insert a boolean field in MongoDB?

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" : 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 More

Advertisements