AmitDiwan has Published 10744 Articles

MongoDB - interpret information on the query plan for the db.collection.find() method

AmitDiwan

AmitDiwan

Updated on 12-May-2020 08:02:14

111 Views

For information on the query plan, use explain() in MongoDB. Let us create a collection with documents −> db.demo637.ensureIndex({ClientName:1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo637.insert({ClientName:"John"}); WriteResult({ "nInserted" : 1 }) > db.demo637.insert({ClientName:"Bob"}); WriteResult({ "nInserted" : 1 ... Read More

MongoDB query to display alternative documents with mapReduce() function and emit even field values

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:54:12

148 Views

Let us create a collection with documents −> db.demo636.insert({id:1}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:2}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:3}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:4}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:5}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:6}); WriteResult({ "nInserted" : 1 })Display all ... Read More

Calculate frequency of duplicate names from NAME field using MongoDB aggregate?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:49:42

205 Views

To calculate frequency, group with $group in aggregate(). Let us create a collection with documents −> db.demo635.insertOne({Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10f06c954c74be91e6cc") } > db.demo635.insertOne({Name:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10f46c954c74be91e6cd") } > db.demo635.insertOne({Name:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10f66c954c74be91e6ce") ... Read More

How to update document with marks value in MongoDB for student David

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:45:25

210 Views

Use forEach() and traverse to find student name David update new marks for the same student. Let us create a collection with documents −> db.demo634.insertOne({Name:"Chris", "Marks":76}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0ea66c954c74be91e6c9") } > db.demo634.insertOne({Name:"Bob", "Marks":67}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0ead6c954c74be91e6ca") } > ... Read More

How to split MongoDB query and skip 5 values?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:38:42

261 Views

To skip values in MongoDB, use skip() along with limit(). For 5 values, use limit(5). Let us create a collection with documents −> db.demo633.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0be76c954c74be91e6c1") } > db.demo633.insertOne({"Value":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bea6c954c74be91e6c2") } > db.demo633.insertOne({"Value":30}); {   ... Read More

Remove values from a matrix like document in MongoDB

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:33:27

136 Views

To remove values from a matrix, use $pull in MongoDB. Let us create a collection with documents −> db.demo632.insertOne( ...    { ...       "arrayMatrix": [ ...          [10, 20], ...          [10, 20], ...          [10, 20], ... ... Read More

Return specific MongoDB embedded document

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:23:41

219 Views

Use $unwind twice for specific embedded document in MongoDB. Let us create a collection with documents −> db.demo631.insert( ...    { ...       id: "101", ...       Info1: [ ...          { ...             CountryName : "US", ... ... Read More

Sort MongoDB documents in descending order

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:19:13

313 Views

To sort documents, use sort() along with find(). Let us create a collection with documents −> db.demo630.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b080e6c954c74be91e6ba") } > db.demo630.insertOne({"Value":200}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b08116c954c74be91e6bb") } > db.demo630.insertOne({"Value":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b08146c954c74be91e6bc") ... Read More

Filter documents in MongoDB using simple query?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:17:36

204 Views

You can use $match. The $match filters the documents to pass only the documents that match the specified condition to the next pipeline stage. Let us create a collection with documents −> db.demo629.insertOne( ...    { ... ...       "Subject": [ ...          "MySQL", ... ... Read More

Query for values (not objects) in list with MongoDB

AmitDiwan

AmitDiwan

Updated on 12-May-2020 07:13:29

144 Views

To query for values in list, use positional operator($) in MongoDB. Let us create a collection with documents −> db.demo628.insertOne({id:1, Name:["Chris", "David", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ae7ea6c954c74be91e6b6") } > db.demo628.insertOne({id:1, Name:["Carol", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ae7f26c954c74be91e6b7") } > db.demo628.insertOne({id:2, Name:["Mike", ... Read More

Advertisements