Merge Array of Documents in MongoDB

AmitDiwan
Updated on 30-Mar-2020 07:19:17

887 Views

To merge, use aggregate() along with $PUSH. Let us create a collection with documents −> db.demo64.insertOne({"Subjects":["MySQL", "MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28762bcfb11e5c34d89936") } > db.demo64.insertOne({"Subjects":["Oracle", "Spring", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28763fcfb11e5c34d89937") }Display all documents from a collection with the help of find() method −> db.demo64.find();This will produce the following output −{ "_id" : ObjectId("5e28762bcfb11e5c34d89936"), "Subjects" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e28763fcfb11e5c34d89937"), "Subjects" : [ "Oracle", "Spring", "Python" ] }Following is the query to merge array of document in MongoDB −> db.demo64.aggregate([ ... { "$group": { ... Read More

Importance of REPL in Java 9

raja
Updated on 27-Mar-2020 17:04:10

629 Views

REPL stands for Read-Eval-Print-Loop. It is a shell where the user types an expression, it's evaluated, and the result returned to the user. The main purpose of using REPL is to interact quickly with Java programs without creating a java file, compile it, and run it. JShell is very useful for the developers and allows us to learn the Java language.Below are some of the features of REPLIt’s built-in in Java 9.We can test any Java expression without a class file, compiling and running it.It autocompletes the methods, just typing the TAB key, as in your editor.We can define methods, and ... Read More

Get Stream from Optional Class in Java 9

raja
Updated on 27-Mar-2020 13:02:36

162 Views

The Optional class provides a container that may or may not contain a non-null value. It has been introduced in Java 8 to reduce the number of places in the code where a NullPointerException has generated. Java 9 added three methods: ifPresentOrElse(),  or(), and stream(), which helps us deal with default values.In the below example, we can get a stream from Optional class using Person classExampleimport java.util.Optional; import java.util.stream.Stream; public class OptionalTest {    public static void main(String args[]) {       getPerson().stream()                  .map(Person::getName)                 ... Read More

MongoDB Aggregation to Sum Product Price with Similar IDs

AmitDiwan
Updated on 27-Mar-2020 12:51:23

413 Views

You need to use $group to group documents with specified _id expression. Let us first create a collection with documents −> db.aggreagateDemo.insertOne({"Product_Id":1, "ProductPrice":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d3c025ddae1f53b621d9") } > db.aggreagateDemo.insertOne({"Product_Id":2, "ProductPrice":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d3c625ddae1f53b621da") } > db.aggreagateDemo.insertOne({"Product_Id":2, "ProductPrice":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d3cb25ddae1f53b621db") } > db.aggreagateDemo.insertOne({"Product_Id":1, "ProductPrice":150}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d3d125ddae1f53b621dc") }Following is the query to display all documents from a collection with the help of find() method −> db.aggreagateDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e06d3c025ddae1f53b621d9"), ... Read More

Properly Use Exists Function in MongoDB Like in SQL

AmitDiwan
Updated on 27-Mar-2020 12:34:34

166 Views

To check for existence of a record, use findOne() in MongoDB. Let us first create a collection with documents −> db.existsAlternateDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d23f9e4dae213890ac5c") } > db.existsAlternateDemo.insertOne({"StudentName":"Chris", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d2559e4dae213890ac5d") } >db.existsAlternateDemo.insertOne({"StudentName":"Chris", "StudentAge":22, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d2689e4dae213890ac5e") }Following is the query to display all documents from a collection with the help of find() method −> db.existsAlternateDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06d23f9e4dae213890ac5c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e06d2559e4dae213890ac5d"), "StudentName" : "Chris", "StudentAge" : 21 } { ... Read More

MongoDB Query to Sum Specific Fields

AmitDiwan
Updated on 27-Mar-2020 12:32:18

2K+ Views

To sum specific fields, use aggregate along with $sum. Let us first create a collection with documents −> db.getSumOfFieldsDemo.insertOne({"Customer_Id":101, "Price":50, "Status":"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06cec29e4dae213890ac55") } > db.getSumOfFieldsDemo.insertOne({"Customer_Id":102, "Price":200, "Status":"Inactive"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ced19e4dae213890ac56") } > db.getSumOfFieldsDemo.insertOne({"Customer_Id":101, "Price":3000, "Status":"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06cedd9e4dae213890ac57") } > db.getSumOfFieldsDemo.insertOne({"Customer_Id":103, "Price":400, "Status":"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06cee79e4dae213890ac58") }Following is the query to display all documents from a collection with the help of find() method −> db.getSumOfFieldsDemo.find().pretty();This will produce the following output −{    "_id" : ... Read More

Insert in MongoDB Without Duplicates

AmitDiwan
Updated on 27-Mar-2020 12:29:48

8K+ Views

To insert records in MongoDB and avoid duplicates, use “unique:true”. Let us first create a collection with documents.Here, we are trying to add duplicate records −> db.insertWithoutDuplicateDemo.createIndex({"StudentFirstName":1}, { unique: true } ); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.insertWithoutDuplicateDemo.insert({"StudentFirstName":"Chris"}, { upsert: true }); WriteResult({ "nInserted" : 1 }) > db.insertWithoutDuplicateDemo.insert({"StudentFirstName":"David"}, { upsert: true }); WriteResult({ "nInserted" : 1 }) > db.insertWithoutDuplicateDemo.insert({"StudentFirstName":"Chris"}, { upsert: true }); WriteResult({    "nInserted" : 0,    "writeError" : {       "code" : 11000,       "errmsg" : "E11000 duplicate ... Read More

Apply Condition Inside Subset in MongoDB Aggregation

AmitDiwan
Updated on 27-Mar-2020 12:26:20

350 Views

To apply a condition, use $setIsSubset. Let us first create a collection with documents −> db.subsetDemo.insertOne({"StudentName":"Chris", "StudentFavouriteSubject":["Java", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e063e49150ee0e76c06a052") } > db.subsetDemo.insertOne({"StudentName":"Chris", "StudentFavouriteSubject":["Java", "Python", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e063e4f150ee0e76c06a053") }Following is the query to display all documents from a collection with the help of find() method −> db.subsetDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e063e49150ee0e76c06a052"),    "StudentName" : "Chris",    "StudentFavouriteSubject" : [       "Java",       "Python"    ] } {    "_id" : ObjectId("5e063e4f150ee0e76c06a053"),    "StudentName" : "Chris",   ... Read More

Getting MongoDB Results from the Previous Month

AmitDiwan
Updated on 27-Mar-2020 12:23:00

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

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

Advertisements