Found 1349 Articles for MongoDB

Get specific elements from embedded array in MongoDB?

AmitDiwan
Updated on 12-May-2020 08:15:34

342 Views

To get specific elements, use $match with dot notation. Let us create a collection with documents −> db.demo641.insert( ...    { ...       ProductId:101, ...       "ProductInformation": ...      (                            [ ...          { ...             ProductName:"Product-1", ...             "ProductPrice":1000 ...          }, ...          { ...             ProductName:"Product-2", ...             "ProductPrice":500 ... ... Read More

Projection of arrays to get the first array element from MongoDB documents

AmitDiwan
Updated on 12-May-2020 08:11:50

546 Views

If you want the first element from array, you can use $slice along with $gte. Let us create a collection with documents −> db.demo640.insertOne({Name:"John", "Scores":[80, 90, 75]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c2eb86c954c74be91e6e0") } > db.demo640.insertOne({Name:"Chris", "Scores":[85, 70, 89]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c2ece6c954c74be91e6e1") }Display all documents from a collection with the help of find() method −> db.demo640.find();This will produce the following output −{ "_id" : ObjectId("5e9c2eb86c954c74be91e6e0"), "Name" : "John", "Scores" : [ 80, 90, 75 ] } { "_id" : ObjectId("5e9c2ece6c954c74be91e6e1"), "Name" : "Chris", "Scores" : [ 85, 70, 89 ] }Following ... Read More

MongoDB aggregation framework with group query example?

AmitDiwan
Updated on 12-May-2020 08:10:13

99 Views

For this, use $group in MongoDB aggregation. Let us create a collection with documents −> db.demo639.insertOne( ...    { ...       "_id" : 1, ...       "CountryName" : "US", ...       "Info1" : { ...          "Name" : "Chris", ...          "SubjectName" : "MySQL", ...          "Marks" : 78 ...       }, ...       "Info2" : { ...          "Name" : "Chris", ...          "SubjectName" : "MySQL", ...          "Marks" : 78 ... Read More

Filtering MongoDB items by fields and subfields?

AmitDiwan
Updated on 12-May-2020 08:05:22

420 Views

To filter items by fields and subfields, use dot notation. Let us create a collection with documents −> db.demo638.insert({Name:"Chris"}); WriteResult({ "nInserted" : 1 }) > db.demo638.insert({Name:"David", details:{Subject:"MongoDB"}}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo638.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e9c28666c954c74be91e6de"), "Name" : "Chris" }    {       "_id" : ObjectId("5e9c28866c954c74be91e6df"),       "Name" : "David",       "details" : {          "Subject" : "MongoDB"    } }Following is the query to filter items by multiple fields and subfields −> ... Read More

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

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

63 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 }) > db.demo637.insert({ClientName:"Johnson"}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo637.find();This will produce the following output −{ "_id" : ObjectId("5e9c26916c954c74be91e6db"), "ClientName" : "John" } { "_id" : ObjectId("5e9c26936c954c74be91e6dc"), "ClientName" : "Bob" } { "_id" : ObjectId("5e9c269d6c954c74be91e6dd"), "ClientName" : "Johnson" }Case ... Read More

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

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

113 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 documents from a collection with the help of find() method −> db.demo636.find();This will produce the following output −{ "_id" : ObjectId("5e9c127b6c954c74be91e6d2"), "id" : 1 } { "_id" : ObjectId("5e9c127e6c954c74be91e6d3"), "id" : 2 } { "_id" : ObjectId("5e9c127f6c954c74be91e6d4"), "id" : 3 } { "_id" : ObjectId("5e9c12816c954c74be91e6d5"), "id" : 4 } { ... Read More

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

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

132 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") } > db.demo635.insertOne({Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10f86c954c74be91e6cf") } > db.demo635.insertOne({Name:"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10fb6c954c74be91e6d0") } > db.demo635.insertOne({Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c10fc6c954c74be91e6d1") }Display all documents from a collection with the help of find() method −> db.demo635.find();This ... Read More

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

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

168 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") } > db.demo634.insertOne({Name:"David", "Marks":37}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0eb76c954c74be91e6cb") }Display all documents from a collection with the help of find() method −> db.demo634.find();This will produce the following output −{ "_id" : ObjectId("5e9c0ea66c954c74be91e6c9"), "Name" : "Chris", "Marks" : 76 } { "_id" : ObjectId("5e9c0ead6c954c74be91e6ca"), "Name" : "Bob", "Marks" : ... Read More

How to split MongoDB query and skip 5 values?

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

225 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}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bec6c954c74be91e6c3") } > db.demo633.insertOne({"Value":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bef6c954c74be91e6c4") } > db.demo633.insertOne({"Value":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bf16c954c74be91e6c5") } > db.demo633.insertOne({"Value":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bf36c954c74be91e6c6") } > db.demo633.insertOne({"Value":70}); {    "acknowledged" : ... Read More

Remove values from a matrix like document in MongoDB

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

89 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], ...          [10, 20], ...          [10, 20], ...          [10, 20] ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b27b46c954c74be91e6c0") }Display all documents from a collection with the help of find() ... Read More

Advertisements