Compare Two Fields in MongoDB During Operations

AmitDiwan
Updated on 01-Apr-2020 14:06:46

316 Views

To compare two fields, use $where in MongoDB. Let us first create a collection with documents −> db.demo7.insertOne({"FirstName1":"JOHN", "FirstName2":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0ccd1a25ddae1f53b6222f") } > db.demo7.insertOne({"FirstName1":"Carol", "FirstName2":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0ccd2725ddae1f53b62230") } > db.demo7.insertOne({"FirstName1":"bob", "FirstName2":"BOB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0ccd3225ddae1f53b62231") }Following is the query to display all documents from a collection with the help of find() method −> db.demo7.find();This will produce the following output −{ "_id" : ObjectId("5e0ccd1a25ddae1f53b6222f"), "FirstName1" : "JOHN", "FirstName2" : "John" } { "_id" : ObjectId("5e0ccd2725ddae1f53b62230"), "FirstName1" : "Carol", "FirstName2" : "Mike" } ... Read More

Get Infinity Result for AVG in Aggregate Query

AmitDiwan
Updated on 01-Apr-2020 14:05:10

150 Views

For this, you can use aggregate(). Let us first create a collection with documents with a vlaue as -infinity −> db.demo5.insertOne({ "_id" : 100, "seq" : 10, "Value" : -Infinity }); { "acknowledged" : true, "insertedId" : 100 } > db.demo5.insertOne({ "_id" : 101, "seq" : 10, "Value" : 50 }); { "acknowledged" : true, "insertedId" : 101 } > db.demo5.insertOne({ "_id" : 102, "seq" : 20, "Value" : 60 }); { "acknowledged" : true, "insertedId" : 102 } > db.demo5.insertOne({ "_id" : 103, "seq" : 20, "Value" : 50 }); { "acknowledged" : true, "insertedId" : 103 }Following is ... Read More

Push an Element into Array in MongoDB

AmitDiwan
Updated on 01-Apr-2020 14:04:32

401 Views

To push an element into array, use $push. Let us first create a collection with documents −> db.demo6.insertOne({"ListOfNames":["Bob", "David", "Mike", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0b6b3f25ddae1f53b62228") }Following is the query to display all documents from a collection with the help of find() method −> db.demo6.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e0b6b3f25ddae1f53b62228"),    "ListOfNames" : [       "Bob",       "David",       "Mike",       "Sam"    ] }Here is the query to push an element into array in MongoDB −> db.demo6.update({ _id: ObjectId("5e0b6b3f25ddae1f53b62228") }, { $push: { ... Read More

Is MongoDB Able to Index a Null Value?

AmitDiwan
Updated on 01-Apr-2020 12:08:52

844 Views

Yes, MongoDB can easily index a null value. Let us first create a collection with documents −> db.demo170.createIndex({"Value":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo170.insert({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo170.insert({"Value":null}); WriteResult({ "nInserted" : 1 }) > db.demo170.insert({"Value":90}); WriteResult({ "nInserted" : 1 }) > db.demo170.insert({"Value":78}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo170.find();This will produce the following output −{ "_id" : ObjectId("5e369b789e4f06af551997da"), "Value" : 100 } { "_id" : ObjectId("5e369b7c9e4f06af551997db"), "Value" : null } { ... Read More

Fetch All the IDs of MongoDB Documents

AmitDiwan
Updated on 01-Apr-2020 12:04:20

2K+ Views

To fetch all the ids, simply use find() in MongoDB. Let us create a collection with documents −> db.demo169.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36975e9e4f06af551997d7") } > db.demo169.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3697629e4f06af551997d8") } > db.demo169.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3697679e4f06af551997d9") }Display all documents from a collection with the help of find() method −> db.demo169.find();This will produce the following output −{ "_id" : ObjectId("5e36975e9e4f06af551997d7"), "StudentName" : "Chris" } { "_id" : ObjectId("5e3697629e4f06af551997d8"), "StudentName" : "Bob" } { "_id" : ObjectId("5e3697679e4f06af551997d9"), "StudentName" : "David" }Following is the query to get ... Read More

Incrementing a Date in JavaScript to Update MongoDB

AmitDiwan
Updated on 01-Apr-2020 12:02:19

134 Views

To increment a date, use setDate, getDate() and perform increment operation. Let us first create a collection with documents −> db.demo168.insertOne({"DueDate":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3695ae9e4f06af551997d6") }Display all documents from a collection with the help of find() method −> db.demo168.find();This will produce the following output −{ "_id" : ObjectId("5e3695ae9e4f06af551997d6"), "DueDate" : null }Following is the query to increment a date in JavaScript, in order to update MongoDB −> var t = new Date(); > t.setDate(t.getDate()+1); 1580722089017 > > var dat = new Date(); > dat .setDate(dat .getDate()+2); 1580808489085 > > db.demo168.update( ...    { "_id": ObjectId("5e3695ae9e4f06af551997d6") ... Read More

MongoDB Query to Get Minimum and Maximum Value from Documents Including Duplicates

AmitDiwan
Updated on 01-Apr-2020 12:00:28

345 Views

For this, use aggregate() and $group. To get minimum and maximum value, use $min and $max.Let us create a collection with documents −> db.demo167.insertOne({"Score":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693a79e4f06af551997d1") } > db.demo167.insertOne({"Score":80}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693ab9e4f06af551997d2") } > db.demo167.insertOne({"Score":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693ad9e4f06af551997d3") } > db.demo167.insertOne({"Score":90}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693b09e4f06af551997d4") } > db.demo167.insertOne({"Score":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3693b69e4f06af551997d5") }Display all documents from a collection with the help of find() method −> db.demo167.find();This will produce the following output ... Read More

MongoDB Query to Return Specific Fields as Phone Numbers Array

AmitDiwan
Updated on 01-Apr-2020 11:58:22

390 Views

Let us create a collection with documents −> db.demo166.insertOne({"details" : { "UserName" : "Chris", "UserAge":29, "PhoneDetails" : { "PhoneNumber" : "98646463533" } } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e368b159e4f06af551997cf") } > db.demo166.insertOne({"details" : { "UserName" : "David", "UserAge":21, "PhoneDetails" : { "PhoneNumber" : "87664534654" } } }); {    "acknowledged" : true,    "insertedId" : ObjectId("5e368b159e4f06af551997d0") }Display all documents from a collection with the help of find() method −> db.demo166.find();This will produce the following output −{ "_id" : ObjectId("5e368b159e4f06af551997cf"), "details" : { "UserName" : "Chris", "UserAge" : 29, "PhoneDetails" : { "PhoneNumber" : "98646463533" } } ... Read More

Remove a Single Document in MongoDB

AmitDiwan
Updated on 01-Apr-2020 11:56:50

136 Views

To remove only a single document in MongoDB, use remove(). Let us create a collection with documents −> db.demo165.insertOne({"ClientId":101, "ClientName":"Chris", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36895c9e4f06af551997cc") } > db.demo165.insertOne({"ClientId":102, "ClientName":"Bob", "ClientAge":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3689659e4f06af551997cd") } > db.demo165.insertOne({"ClientId":103, "ClientName":"David", "ClientAge":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36896d9e4f06af551997ce") }Display all documents from a collection with the help of find() method −> db.demo165.find();This will produce the following output −{ "_id" : ObjectId("5e36895c9e4f06af551997cc"), "ClientId" : 101, "ClientName" : "Chris", "ClientAge" : 34 } { "_id" : ObjectId("5e3689659e4f06af551997cd"), "ClientId" : 102, "ClientName" : ... Read More

Use MongoDB Aggregate to Sort Data

AmitDiwan
Updated on 01-Apr-2020 11:54:50

215 Views

Use aggregate() and within that to sort, use $sort in MongoDB. Let us create a collection with documents −> db.demo164.insertOne({"StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e36883d9e4f06af551997c8") } > db.demo164.insertOne({"StudentAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3688409e4f06af551997c9") } > db.demo164.insertOne({"StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3688429e4f06af551997ca") } > db.demo164.insertOne({"StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3688439e4f06af551997cb") }Display all documents from a collection with the help of find() method −> db.demo164.find();This will produce the following output −{ "_id" : ObjectId("5e36883d9e4f06af551997c8"), "StudentAge" : 24 } { "_id" : ObjectId("5e3688409e4f06af551997c9"), "StudentAge" : 25 } ... Read More

Advertisements