Articles on Trending Technologies

Technical articles with clear explanations and examples

Is there a MongoDB query to concatenate deep sub-lists?

AmitDiwan
AmitDiwan
Updated on 30-Mar-2020 150 Views

Concatenate deep sub-lists using aggregate() along with $unwind. Let us create a collection with documents −> db.demo70.insertOne( ...    { ... ...       "first" : [ ...          { ...             "details" : { ...                "second" : [ ...                   { ...                      "StudentDetails" : { ...                      "Score" : 10 ...           ...

Read More

MongoDB, finding all documents where property id equals to record id?

AmitDiwan
AmitDiwan
Updated on 30-Mar-2020 305 Views

For this, use $where and compare with ==. Let us create a collection with documents −> db.demo69.insertOne( ... { ...    "_id" : ObjectId("507c7f79bcf86cd7994f6c0e"), ... "Details" : { ... ...    "id" : ObjectId("507c7f79bcf86cd7994f6c0e") ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("507c7f79bcf86cd7994f6c0e") } > db.demo69.insertOne( ... { ...    "_id" : ObjectId("507c7f79bcf86cd7994f6c0f"),    "Details" : { ...    "id" :ObjectId("507c7f79bcf86cd7994f6c0a") ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("507c7f79bcf86cd7994f6c0f") }Display all documents from a collection with the help of find() method −> db.demo69.find();This will produce ...

Read More

MongoDB aggregate query to sort

AmitDiwan
AmitDiwan
Updated on 30-Mar-2020 280 Views

To sort, use $match along with aggregate. Let us create a collection with documents −> db.demo67.insertOne({"StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e289edf602d9a2ff1828ed8") } > db.demo67.insertOne({"StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e289ee1602d9a2ff1828ed9") } > db.demo67.insertOne({"StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e289ee3602d9a2ff1828eda") }Display all documents from a collection with the help of find() method −> db.demo67.find();This will produce the following output −{ "_id" : ObjectId("5e289edf602d9a2ff1828ed8"), "StudentAge" : 23 } { "_id" : ObjectId("5e289ee1602d9a2ff1828ed9"), "StudentAge" : 21 } { "_id" : ObjectId("5e289ee3602d9a2ff1828eda"), "StudentAge" : 24 }Following is the query to sort with aggregate in ...

Read More

How to get the index of an array element in older versions on MongoDB?

AmitDiwan
AmitDiwan
Updated on 30-Mar-2020 204 Views

To get index of an array element, use $indexOfArray. Let us create a collection with documents −> db.demo65.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28990ecfb11e5c34d89938") } > db.demo65.insertOne({"ListOfValues":[50, 60, 70, 100]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28991ecfb11e5c34d89939") } > db.demo65.insertOne({"ListOfValues":[30, 40, 89, 91, 98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28992bcfb11e5c34d8993a") }Display all documents from a collection with the help of find() method −> db.demo65.find();This will produce the following output −{ "_id" : ObjectId("5e28990ecfb11e5c34d89938"), "ListOfValues" : [ 10, 20, 30 ] } { "_id" : ObjectId("5e28991ecfb11e5c34d89939"), "ListOfValues" : [ 50, 60, ...

Read More

What is the importance of REPL in Java 9?

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

How to get a stream from Optional class in Java 9?

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

How to properly use 'exist' function in MongoDB like in SQL?\\n

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

Insert in MongoDB without duplicates

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 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 a condition inside subset in MongoDB Aggregation?

AmitDiwan
AmitDiwan
Updated on 27-Mar-2020 373 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
Showing 48601–48610 of 61,248 articles
Advertisements