AmitDiwan has Published 10744 Articles

In MongoDB will limit() increase query speed?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 10:04:51

115 Views

No, using LIMIT() decreases the bandwidth consumption and it does not increase query speed. Let us see an example and create a collection with documents −> db.demo197.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3afde803d395bdc21346d8") } > db.demo197.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3afdef03d395bdc21346d9") } > ... Read More

Update key value where different key equals some value in MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 10:01:56

518 Views

Let us create a collection with documents −> db.demo196.insertOne( ...   { ... ...      "Id" : "101", ...      "details" : [ ...         { ...            "FirstName" : "Chris", ...            "LastName" : "Brown", ... ... Read More

Is it possible to return a list of specific values from a query in MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 09:57:42

410 Views

To return a list of specific values, use map(). Let us create a collection with documents −> db.demo195.insertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3af3f203d395bdc21346d4") } > db.demo195.insertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3af3f703d395bdc21346d5") } > db.demo195.insertOne({"Subject":"Java"}); {    "acknowledged" : true,    "insertedId" : ... Read More

MongoDB query in object of arrays

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 09:55:41

190 Views

Let us first create a collection with documents −> db.demo194.insertOne( ...   { ...      "_id": 101, ...      "details": { ...         "otherDetails": { ...            "List1": ["MongoDB", "MySQL"], ...            "List2": ["Java"], ...     ... Read More

How to improve the execution time of a query in MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:34:54

261 Views

To improve the execution time of a query, use index along with unique:true. Let us create a collection with documents −> db.demo193.createIndex({"LastName":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo193.insertOne({"FirstName":"John", "LastName":"Doe"}); {    "acknowledged" : true,   ... Read More

MongoDB query to group several fields using aggregation framework?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:30:20

238 Views

To group several fields, use $group in MongoDB. Let us create a collection with documents −> db.demo192.insertOne({"Name":"Chris", "Age":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3adb9f03d395bdc21346cd") } > db.demo192.insertOne({"Name":"David", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3adba103d395bdc21346ce") } > db.demo192.insertOne({"Name":"Chris", "Age":22}); {    "acknowledged" : true,   ... Read More

How to get values of cursor in MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:28:37

672 Views

To get values of cursor in MongoDB, use hasNext(). Let us create a collection with documents −> db.demo191.insertOne({"EmployeeId":1, "EmployeeName":"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ad95303d395bdc21346c5") } > db.demo191.insertOne({"EmployeeId":2, "EmployeeName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3ad95f03d395bdc21346c6") } > db.demo191.insertOne({"EmployeeId":1, "EmployeeName":"John Doe"}); {   ... Read More

MongoDB aggregation with multiple keys

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:26:08

597 Views

To implement aggregation with multiple keys, use aggregate() along with $group. Let us create a collection with documents −> db.demo190.insertOne( ...   { ... ...      "DueDate" : ISODate("2020-01-01"), ...      "Value" : 10, ...      "Name" : "Chris" ...   } ...); {    "acknowledged" : ... Read More

MongoDB query to sort by the sum of specified object inside inner array?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:22:39

316 Views

To sort by the sum of specified object inside inner array, use $match along with $sort. Let us create a collection with documents −> db.demo189.insertOne( ...   { ...      "_id" : 100, ...      "List" : [ ...         { ...       ... Read More

What kind of MongoDB query finds same value multiple times in an array?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 08:16:21

451 Views

To find same value multiple times, use $where in MongoDB. Let us create a collection with documents −> db.demo188.insertOne( ...   { ...      "ListOfData":[ ...         {"Data": 100}, ...         {"Data": 200}, ...         {"Data": 100} ...     ... Read More

Advertisements