To implement aggregation with multiple keys, use aggregate() along with $group. Let us create a collection with documents −> db.demo190.insertOne( ... { ... ... "DueDate" : ISODate("2020-01-01"), ... "Value" : 10, ... "Name" : "Chris" ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad76403d395bdc21346bf") } > > db.demo190.insertOne( ... { ... ... "DueDate" : ISODate("2020-02-05"), ... "Value" : 30, ... "Name" : "David" ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad76403d395bdc21346c0") } > db.demo190.insertOne( ... { ... Read More
To sort by the sum of specified object inside inner array, use $match along with $sort. Let us create a collection with documents −> db.demo189.insertOne( ... { ... "_id" : 100, ... "List" : [ ... { ... "Value" : 10 ... }, .. .{ ... "Value" : 20 ... }, ... { ... "Value" : 10 ... ... Read More
Flow API is official support for reactive streams specification since Java 9. It is a combination of both Iterator and Observer patterns. The Flow API is an interoperation specification and not an end-user API like RxJava.Flow API consists of four basic interfaces:Subscriber: The Subscriber subscribes to Publisher for callbacks.Publisher: The Publisher publishes the stream of data items to the registered subscribers.Subscription: The link between publisher and subscriber.Processor: The processor sits between Publisher and Subscriber, and transforms one stream to another.In the below example, we have created a basic subscriber that asks for one data object, prints it and asks for one more. ... Read More
To find same value multiple times, use $where in MongoDB. Let us create a collection with documents −> db.demo188.insertOne( ... { ... "ListOfData":[ ... {"Data": 100}, ... {"Data": 200}, ... {"Data": 100} ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3ad1c203d395bdc21346bd") } > db.demo188.insertOne( ... { ... "ListOfData":[ ... {"Data": 100}, ... {"Data": 200}, ... {"Data": 300} ... } ...); { ... Read More
Let us first create a collection with documents −> db.demo187.insertOne( ... { ... "_id" : "101", ... "Details" : [ ... { "Subject" : "MongoDB" }, ... { "Subject" : "MySQL" } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "101" } > db.demo187.insertOne( ... { ... "_id" : "102", ... "Details" : [ ... { } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "102" ... Read More
Let us first create a collection with documents −> db.demo186.insertOne({"UserEmailId":"JOHN@GMAIL.COM", "UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d769e4f06af55199808") } > db.demo186.insertOne({"UserEmailId":"chris@gmail.com", "UserName":"chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d879e4f06af55199809") } > db.demo186.insertOne({"UserEmailId":"DAVID@GMAIL.COM", "UserName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d979e4f06af5519980a") }Display all documents from a collection with the help of find() method −> db.demo186.find();This will produce the following output −{ "_id" : ObjectId("5e399d769e4f06af55199808"), "UserEmailId" : "JOHN@GMAIL.COM", "UserName" : "John" } { "_id" : ObjectId("5e399d879e4f06af55199809"), "UserEmailId" : "chris@gmail.com", "UserName" : "chris" } { "_id" : ObjectId("5e399d979e4f06af5519980a"), "UserEmailId" : "DAVID@GMAIL.COM", "UserName" : "David" }Following is the ... Read More
To fetch records with $or on the basis of existence, use $or along with $exists. Let us create a collection with documents −>db.demo185.insertOne({_id:101, details:{Name:"Chris", Score:78, Subjects:{"Name":"MySQL"}}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo185.insertOne({_id:102, details:{Name:"Bob", Score:78}}); { "acknowledged" : true, "insertedId" : 102 } >db.demo185.insertOne({_id:103, details:{Name:"David", Score:78, Subjects:{"Name":"MongoDB"}}}); { "acknowledged" : true, "insertedId" : 103 }Display all documents from a collection with the help of find() method −> db.demo185.find();This will produce the following output −{ "_id" : 101, "details" : { "Name" : "Chris", "Score" : 78, "Subjects" : { "Name" : "MySQL" } } } { "_id" ... Read More
To count, you can also use aggregate() along with $sum. Let us create a collection with documents −> db.demo184.insertOne({"Names":["Chris", "David", "Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3999fb9e4f06af55199805") } > db.demo184.insertOne({"Names":["Chris", "Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e399a0d9e4f06af55199806") } > db.demo184.insertOne({"Names":["Chris", "Bob", "Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e399a209e4f06af55199807") }Display all documents from a collection with the help of find() method −> db.demo184.find();This will produce the following output −{ "_id" : ObjectId("5e3999fb9e4f06af55199805"), "Names" : [ "Chris", "David", "Bob" ] } { "_id" : ObjectId("5e399a0d9e4f06af55199806"), "Names" : [ "Chris", "Mike" ] } { ... Read More
To display subdocuments in one line, use $unwind along with aggregate(). Let us create a collection with documents −> db.demo183.insertOne( ... { ... "_id": "110", ... "DueDate": ISODate("2020-02-04T01:10:42.000Z"), ... "ProductDetails": [ ... { ... "ProductName": "Product-1", ... "isAvailable": true ... }, ... { ... "ProductName": "Product-2", ... "isAvailable": false ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "110" }Display all documents from a collection with the ... Read More
To select one field if the other is null, use $ifNull. Let us create a collection with documents −> db.demo182.insertOne({"FirstName":"Chris", "LastName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e398ea19e4f06af55199802") } > db.demo182.insertOne({"FirstName":null, "LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e398ead9e4f06af55199803") } > > db.demo182.insertOne({"FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e398ebf9e4f06af55199804") }Display all documents from a collection with the help of find() method −> db.demo182.find();This will produce the following output −{ "_id" : ObjectId("5e398ea19e4f06af55199802"), "FirstName" : "Chris", "LastName" : null } { "_id" : ObjectId("5e398ead9e4f06af55199803"), "FirstName" : null, "LastName" : "Miller" } { "_id" ... Read More