Articles on Trending Technologies

Technical articles with clear explanations and examples

Getting MongoDB results from the previous month\\n

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 1K+ Views

At first, get the current month and subtract by 1 to fetch previous month records. Let us first create a collection with documents −> db.findOneMonthAgoData.insertOne({"CustomerName":"Chris", "PurchaseDate":new ISODate("2019-12-26")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04e16c150ee0e76c06a04f") } > db.findOneMonthAgoData.insertOne({"CustomerName":"David", "PurchaseDate":new ISODate("2019-11-26")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04e178150ee0e76c06a050") } > db.findOneMonthAgoData.insertOne({"CustomerName":"Bob", "PurchaseDate":new ISODate("2020-11-26")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04e186150ee0e76c06a051") }Following is the query to display all documents from a collection with the help of find() method −> db.findOneMonthAgoData.find();This will produce the following output −{ "_id" : ObjectId("5e04e16c150ee0e76c06a04f"), "CustomerName" : "Chris", "PurchaseDate" : ISODate("2019-12-26T00:00:00Z") } { ...

Read More

How can I aggregate nested documents in MongoDB?\\n

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 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

How to add a column in MongoDB collection?

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 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

How to get specific data in different formats with MongoDB?

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 157 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

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

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 383 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

How to get Java and OS version, vendor details in JShell in Java 9?

raja
raja
Updated on 27-Mar-2020 612 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 during MongoDB aggregation operation

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 381 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

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

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 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") } > db.countandsumdemo.insertOne({"Value":100, "created_at":ISODate('2019-06-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e8af5e889d7a51994fc") }Following is the query to display all documents from a collection with the help of find() method −> db.countandsumdemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e038e6df5e889d7a51994fa"),    "Value" : 10,    "created_at" : ISODate("2019-10-11T00:00:00Z") ...

Read More

Build index in the background with MongoDB

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 3K+ Views

To create index in the background, use createIndex() method and set “background: true” as in the below syntax −db.yourCollectionName.createIndex({"yourFieldName1":1,"yourFieldName2":1},{background: true} );Let us implement the above syntax in order to create index and set background −> db.indexCreationDemo.createIndex({"StudentName":1,"StudentAge":1},{background: true} ); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Let us display the indexes −> db.indexCreationDemo.getIndexes();This will produce the following output −[    {       "v" : 2,       "key" : {          "_id" : 1       },       "name" : "_id_",       "ns" : "web.indexCreationDemo"    },    {       "v" : 2,       "key" : {          "StudentName" : 1,          "StudentAge" : 1       },       "name" : "StudentName_1_StudentAge_1",       "ns" : "web.indexCreationDemo",       "background" : true    } ]

Read More

Fix: MongoDB Robomongo: db.data.find(…).collation is not a function?

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 138 Views

The collation introduced in version MongoDB 3.4. Maybe, you implemented collation in a previous version.For our example, we are using MongoDB version 4.0.5. Following is the query to check the current version on system −> db.version()This will produce the following output −4.0.5Let us first create a collection with documents −> db.collationExample.createIndex({Value: 1}, {collation: {locale: "en", strength: 1}}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.collationExample.insertOne({'Value':'x'}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038a3cf5e889d7a51994f5") } > db.collationExample.insertOne({'Value':'X'}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038a48f5e889d7a51994f6") } > ...

Read More
Showing 48611–48620 of 61,248 articles
Advertisements