AmitDiwan has Published 10744 Articles

How can I aggregate nested documents in MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:20:33

1K+ Views

To aggregate nested documents in MongoDB, you can use $group. Let us first create a collection with documents −> db.aggregateDemo.insertOne( ...    { ...       "ProductInformation": [ ...          { ...             "Product1": [ ...           ... Read More

Update _id field in MongoDB

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:09:13

482 Views

To update, just save new ID and remove the old one using remove(). Let us first create a collection with documents −> db.updatingDemo.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04dae5150ee0e76c06a04b") } > db.updatingDemo.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04dae7150ee0e76c06a04c") }Following is the query to display ... Read More

How to add a column in MongoDB collection?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:05:42

9K+ Views

To add a column, you need to update the collection. The syntax is as follows −db.getCollection(yourCollectionName).update({}, {$set: {"yourColumnName": "yourValue"}}, false, true);To understand the above syntax, let us create a collection with documents −> db.addColumnDemo.insertOne({"StudentId":101, "StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d66af5e889d7a519950f") } > db.addColumnDemo.insertOne({"StudentId":102, "StudentName":"Robert"}); {   ... Read More

MongoDB query to test if a value is in array?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:02:38

451 Views

To check for a specific value, use $in. Let us first create a collection with documents −> db.testInArray.insertOne({"ListOfNumbers":[10, 56, 78, 90, 32]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d42df5e889d7a519950d") } > db.testInArray.insertOne({"ListOfNumbers":[56, 78, 91, 100]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d588f5e889d7a519950e") }Following is the query ... Read More

How to get specific data in different formats with MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 11:59:38

127 Views

For this, simply use find(). For a different format, use pretty(). Let us first create a collection with documents −> db.getSpecificData.insertOne( ... { ...    "StudentName": "John", ...    "Information": { ...       "FatherName": "Chris", ...       "Place": { ...          "CountryName": "US", ... Read More

MongoDB - How to copy rows into a newly created collection?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 11:54:39

362 Views

To copy rows into another collection, use MongoDB. The syntax is as follows wherein “yourOldCollectionName” is the old collection, whereas where this collection will get copied is our new collection i.e. “yourNewCollectionName” −db.yourOldCollectionName.aggregate([{ $sample: { size: 333333 }}, {$out: "yourNewCollectionName"} ], {allowDiskUse: true});Let us first create a collection with documents ... Read More

Why is MongoDB taking too much time to find the record?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 11:52:36

232 Views

In this case, use the concept of index on a particular field. Let us first create a collection with documents. Here, we have created index as well using createIndex() −> db.decreasetimeusingindex.createIndex({"StudentName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > ... Read More

Extract a particular element from a nested array in MongoDB

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 11:49:28

223 Views

Extract a particular element from a nested array with the help of dot(.) notation. Let us first create a collection with documents −> db.extractParticularElementDemo.insertOne( ...    { ...       "_id" : 101, ...       "StudentName" : "John", ...       "StudentInformation" : [ ...   ... Read More

Replace value with a string literal during MongoDB aggregation operation

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 11:40:04

341 Views

Use MongoDB $literal to set a string literal. Let us first create a collection with documents −>db.replacevaluedemo.insertOne({"StudentName":"Chris", "StudentFavouriteSubject":{"TeacherName":"Bob", "SubjectCode":"MySQL111"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0390a3f5e889d7a51994fd") } >db.replacevaluedemo.insertOne({"StudentName":"Mike", "StudentFavouriteSubject":{"TeacherName":"David", "SubjectCode":"3221Java"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0390b8f5e889d7a51994fe") }Following is the query to display all documents from ... Read More

How to count and sum a field between 2 dates in MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 11:37:24

1K+ Views

Use aggregation $gte and $lte along with $sum to count and sum a field between 2 dates. Let us first create a collection with documents −> db.countandsumdemo.insertOne({"Value":10, "created_at":ISODate('2019-10-11')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e6df5e889d7a51994fa") } > db.countandsumdemo.insertOne({"Value":50, "created_at":ISODate('2019-01-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e77f5e889d7a51994fb") ... Read More

Advertisements