Add New Item in Nested Array with MongoDB

AmitDiwan
Updated on 31-Mar-2020 11:38:23

626 Views

For this, use find() along with update(). Let us create a collection with documents −> db.demo124.insertOne( ...    { ...       "Name" : "John", ...       "Id" : 101, ...       "ProjectDetails" : [{ ...          "ProjectName1" : "Online Book", ...          "ProjectName2" : "Online Banking" ...    }, { ...          "ProjectName1" : "Online Library Management System", ...          "ProjectName2" : "School Management System" ...       }] ...    } ... ); {    "acknowledged" : true,    "insertedId" ... Read More

Search for Documents with Similar Arrays in MongoDB

AmitDiwan
Updated on 31-Mar-2020 11:35:29

239 Views

Let us create a collection with documents −> db.demo123.insertOne({"ListOfSubject":['MySQL', 'MongoDB', 'Java']}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f24ac140daf4c2a3544b8") } > db.demo123.insertOne({"ListOfSubject":['Python', 'MongoDB', 'C']}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f24cd140daf4c2a3544b9") } > db.demo123.insertOne({"ListOfSubject":['MySQL', 'MongoDB', 'C++']}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f24ce140daf4c2a3544ba") }Display all documents from a collection with the help of find() method −> db.demo123.find();This will produce the following output −{ "_id" : ObjectId("5e2f24ac140daf4c2a3544b8"), "ListOfSubject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e2f24cd140daf4c2a3544b9"), "ListOfSubject" : [ "Python", "MongoDB", "C" ] } { "_id" : ObjectId("5e2f24ce140daf4c2a3544ba"), "ListOfSubject" : [ "MySQL", "MongoDB", ... Read More

Get Distinct Levels of Array Field in MongoDB

AmitDiwan
Updated on 31-Mar-2020 11:33:10

214 Views

To get distinct levels of array field, use $addToSet in MongoDB. Let us create a collection with documents −> db.demo122.insertOne({"ListOfValues":[100, 10]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f20f1140daf4c2a3544b6") } > db.demo122.insertOne({"ListOfValues":[240, 10]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f20f7140daf4c2a3544b7") }Display all documents from a collection with the help of find() method −> db.demo122.find();This will produce the following output −{ "_id" : ObjectId("5e2f20f1140daf4c2a3544b6"), "ListOfValues" : [ 100, 10 ] } { "_id" : ObjectId("5e2f20f7140daf4c2a3544b7"), "ListOfValues" : [ 240, 10 ] }Following is the query to get distinct levels of array field in MongoDB −> db.demo122.aggregate([ ...   ... Read More

MongoDB Query to Implement Aggregate Function

AmitDiwan
Updated on 31-Mar-2020 11:31:11

214 Views

Let us first create a collection with documents −> db.demo121.insertOne( ...    { ...       "Id" : 101, ...       "Details" : [ ...          { ...             "SubjectId" : "1", ...             "SubjectName" : "MongoDB", ...             "Score" : 76 ...          }, ...          { ...             "SubjectId" : "2", ...             "SubjectName" : "MySQL", ...         ... Read More

MongoDB Query for Ranking Search Count

AmitDiwan
Updated on 31-Mar-2020 11:27:13

444 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo120.insertOne( ...    { ...       'Name': 'Chris', ...       'Subjects': [ 'MySQL', 'MongoDB', 'Java', 'Python' ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f11aed8f64a552dae6365") } > db.demo120.insertOne( ...    { ...       'Name': 'Bob', ...       'Subjects': [ 'C', 'MongoDB' ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2f11afd8f64a552dae6366") }Display all documents from a collection with the help of find() method −> db.demo120.find();This will produce ... Read More

Importance of Thread onSpinWait Method in Java 9

raja
Updated on 31-Mar-2020 11:24:07

957 Views

Thread.onSpinWait() method has been introduced in Java 9. It is a static method of Thread class and can be optionally called in busy-waiting loops. It allows the JVM to issue processor instructions on some system architectures to improve reaction time in such spin-wait loops, and also reduce the power consumed by the core thread. It can benefit the overall power consumption of a java program and allows other core threads to execute at faster speeds within the same power consumption envelope.Syntaxpublic static void onSpinWait()Examplepublic class ThreadOnSpinWaitTest {    public static void main(final String args[]) throws InterruptedException {       ... Read More

Print Immutable Linked List in Reverse in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:19:35

464 Views

Suppose we have an immutable linked list, we have to print out all values of each node in reverse with the help of the following interface −ImmutableListNode − This is an interface of an immutable linked list, we are given the head of the list.We have to use the following functions to access the linked list −ImmutableListNode.printValue() − This will print value of the current node.ImmutableListNode.getNext() −This will return the next node.So if the list is like: [0, -4, -1, 3, -5], then the output will be [-5, 3, -1, -4, 0]To solve this, we will follow these steps −Define ... Read More

Airplane Seat Assignment Probability in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:18:16

285 Views

Suppose n passengers board an airplane with exactly n seats. If the first passenger has lost the ticket and picks a seat randomly. But after that, the rest of passengers will follow these operations −Take their own seat written in the ticket if it is still available, Pick other seats randomly when they find their seat occupiedSo we have to find what is the probability that the n-th person can get his own seat? So if the input is 2, then the output will be 0.5. So the second person has a probability of 0.5 to get the second seat ... Read More

Get Equal Substrings Within Budget in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:16:56

198 Views

Suppose we have given two strings s and t of the same length. We want to change s to t. Changing the i-th character of s to i-th character of t will assign cost as |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters. We have also given an integer maxCost. We have to find the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with a cost less than or equal to maxCost.So if the input is like s = “abcd” ... Read More

Smallest String with Swaps in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:15:52

400 Views

Suppose we have given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string. We can swap the characters at any pair of indices in the given pairs any number of times as we want. We have to find the lexicographically smallest string that s can be changed to after using the swaps. So if the input is like s = “dcab” and pairs = [[0, 3], [1, 2]], then the output will be “bacd”. Exchange s[0] and s[3], s = "bcad", then exchange s[1] ... Read More

Advertisements