Aggregate Nested Documents in MongoDB

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": [ ...                { ...                   Amount: 50 ...                }, ...                { ...                   Amount: 90 ...         ... Read More

Update ID Field in MongoDB

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

509 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 all documents from a collection with the help of find() method −> db.updatingDemo.find();This will produce the following output −{ "_id" : ObjectId("5e04dae5150ee0e76c06a04b"), "StudentName" : "Robert" } { "_id" : ObjectId("5e04dae7150ee0e76c06a04c"), "StudentName" : "Bob" }Here is the query to update _id in MongoDB −> myDocument = db.updatingDemo.findOne({"StudentName":"Bob"}); { "_id" : ObjectId("5e04dae7150ee0e76c06a04c"), ... Read More

Add Column in MongoDB Collection

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"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d673f5e889d7a5199510") } > db.addColumnDemo.insertOne({"StudentId":103, "StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d67bf5e889d7a5199511") }Following is the query to display all documents from a collection with the help of find() method −> db.addColumnDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e04d66af5e889d7a519950f"),   ... Read More

MongoDB Query to Test if a Value is in Array

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

476 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 to display all documents from a collection with the help of find() method −> db.testInArray.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e04d42df5e889d7a519950d"),    "ListOfNumbers" : [       10,       56,       78,       90,       32   ... Read More

Get Specific Data in Different Formats with MongoDB

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

144 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", ...          "ZipCode":"111344" ...       }, ...       "id": "1" ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e039abdf5e889d7a5199509") } > db.getSpecificData.insertOne( ...    { ...       "StudentName": "Carol", ...       "Information": ... Read More

Copy Rows into a Newly Created Collection in MongoDB

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

376 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 −> db.sourceCollection.insertOne({"EmployeeName":"Robert", "EmployeeSalary":15000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0397c1f5e889d7a5199506") } > db.sourceCollection.insertOne({"EmployeeName":"David", "EmployeeSalary":25000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0397c3f5e889d7a5199507") } > db.sourceCollection.insertOne({"EmployeeName":"Mike", "EmployeeSalary":29000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0397c4f5e889d7a5199508") }Following is the query to display all documents from a collection with ... Read More

Why is MongoDB Taking Too Much Time to Find the Record

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

250 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 } > db.decreasetimeusingindex.insertOne({"StudentName":"John Smith", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e03960af5e889d7a51994ff") } > db.decreasetimeusingindex.insertOne({"StudentName":"John Doe", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e039611f5e889d7a5199500") } > db.decreasetimeusingindex.insertOne({"StudentName":"Adam Smith", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e03961df5e889d7a5199501") }Following is the query to display all documents from a ... Read More

Extract Element from Nested Array in MongoDB

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

245 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" : [ ...          { ...             "Age" : 21, ...             "StudentPersonalInformation" : [ ...                { ...                   "StudentNickName" : "Mike", ...       ... Read More

Get Java and OS Version Vendor Details in JShell in Java 9

raja
Updated on 27-Mar-2020 11:40:08

590 Views

Java 9 introduces the JShell tool for the Java Programming Language. This tool allows us to evaluate code snippets such as declarations, statements, and expressions. We will get the details about Java Version and Vendor, and also get the details about OS Version and Name by using the static method: getProperty() of System class.In the below code snippet, we can able to get the details of Java version, Java Vendor and OS version and OS name in JShell console.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> System.out.println(System.getProperty("java.version")); 9.0.4 jshell> System.out.println(System.getProperty("java.vendor")); Oracle ... Read More

Replace Value with a String Literal in MongoDB Aggregation

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

366 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 a collection with the help of find() method −> db.replacevaluedemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e0390a3f5e889d7a51994fd"),    "StudentName" : "Chris",    "StudentFavouriteSubject" : {       "TeacherName" : "Bob",       "SubjectCode" : "MySQL111"    } } {    "_id" : ObjectId("5e0390b8f5e889d7a51994fe"),    "StudentName" ... Read More

Advertisements