AmitDiwan has Published 10744 Articles

MongoDB: How to query a collection named “version”?

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:21:57

139 Views

For this, use the concept of createCollection() and getCollection(). Following is the query to create a collection with name ‘version’ −> db.createCollection('version'); { "ok" : 1 }Let us first create a collection with documents −> db.getCollection('version').insertOne({"VersionName":"1.0"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e100b47d7df943a7cec4fae") } > db.getCollection('version').insertOne({"VersionName":"1.1"}); {   ... Read More

Find MongoDB documents where elements of an array have a specific value?

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:20:07

164 Views

To match documents in MongoDB, use $elemMatch. Let us first create a collection with documents −> db.demo15.insertOne({"Details":[{"Score":56}, {"Score":78}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f7806d7df943a7cec4fab") } > db.demo15.insertOne({"Details":[{"Score":86}, {"Score":86}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f7817d7df943a7cec4fac") } > db.demo15.insertOne({"Details":[{"Score":97}, {"Score":85}]}); {    "acknowledged" : true,   ... Read More

MongoDB query for array concatenation?

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:18:15

161 Views

To concatenate, use $concatArrays in MongoDB. Let us first create a collection with documents −>db.demo14.insertOne({"ListOfStudent":["Carol", "Mike", "Sam"], "ListOfTeacher":["Robert", "David"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f754bd7df943a7cec4faa") }Following is the query to display all documents from a collection with the help of find() method −> db.demo14.find().pretty();This will produce the ... Read More

How to use collMod in MongoDB runCommand()?

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:16:40

789 Views

The collMod makes it possible to add options to a collection or to modify view definitions. You can use runCommand() along with collMod(). Let us first create a collection with documents −> db.demo13.insertOne({"StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f730ad7df943a7cec4fa6") } > db.demo13.insertOne({"StudentFirstName":"David"}); {    "acknowledged" : true, ... Read More

Query MongoDB for a nested search

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:14:59

233 Views

For nested search, use $and along with $or. Let us first create a collection with documents −> db.demo12.insertOne({"Name":"Chris", "Age":23, "CountryName":"US", "Message":"Hello"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f70a2d7df943a7cec4fa2") } > db.demo12.insertOne({"Name":"David", "Age":21, "CountryName":"US", "Message":"Hello"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f70acd7df943a7cec4fa3") } > db.demo12.insertOne({"Name":"Bob", "Age":23, "CountryName":"AUS", ... Read More

MongoDB query to insert an array element with a condition?

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:13:39

695 Views

Let us first create a collection with documents −>db.demo11.insertOne({"ListOfStudent":[{"StudentName":"Chris", "ListOfScore":[76, 67, 54, 89]}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f6e34d7df943a7cec4fa1") }Following is the query to display all documents from a collection with the help of find() method −> db.demo11.find().pretty();This will produce the following output −{    "_id" : ... Read More

MongoDB query for a single field

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:11:19

764 Views

For a single field, use find(). Let us first create a collection with documents −> db.demo10.insertOne({"StudentId":101, "StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f68a7d7df943a7cec4f9b") } > db.demo10.insertOne({"StudentId":102, "StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f68afd7df943a7cec4f9c") } > db.demo10.insertOne({"StudentId":103, "StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" ... Read More

Update an array element matching a condition using $push in MongoDB

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:09:18

623 Views

For this, use update command and $push. Let us first create a collection with documents −>db.demo9.insertOne({"StudentDetails":[{"StudentName":"Chris", "ListOfSubject":["MySQL", "Java"]}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0f6438d7df943a7cec4f94") }Following is the query to display all documents from a collection with the help of find() method −> db.demo9.find().pretty();This will produce the following ... Read More

How do you compare two fields in MongoDB while performing an operation on one of them?

AmitDiwan

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, ... Read More

How to get a “-Infinity” result for $avg in an aggregate query?

AmitDiwan

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 ... Read More

Advertisements