Conditional Update Depending on Field Matched in MongoDB

AmitDiwan
Updated on 01-Apr-2020 10:58:57

449 Views

For conditional update, use update() and set new value using $set. Let us create a collection with documents −> db.demo150.insertOne({"StudentId":101, "StudentName":"Chris", "StudentMarks":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350dcdfdf09dd6d08539d3") } > db.demo150.insertOne({"StudentId":102, "StudentName":"Chris", "StudentMarks":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350dcefdf09dd6d08539d4") } > db.demo150.insertOne({"StudentId":103, "StudentName":"David", "StudentMarks":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350dcffdf09dd6d08539d5") } > db.demo150.insertOne({"StudentId":104, "StudentName":"Chris", "StudentMarks":38}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350dd0fdf09dd6d08539d6") }Display all documents from a collection with the help of find() method −> db.demo150.find();This will produce the following output −{ "_id" : ObjectId("5e350dcdfdf09dd6d08539d3"), "StudentId" : 101, "StudentName" ... Read More

Core Library Changes in Process API in Java 9

raja
Updated on 01-Apr-2020 10:01:49

146 Views

In Java 9, one can retrieve the PID of the process through a native call and can be achievable through the ProcessHandle. We can also retrieve information about the currently running Java Process (JVM) and Info (inner class of ProcessHandle) class that contains details about the process. We can also return a snapshot of all currently running processes in the system.Exampleimport java.lang.ProcessHandle.Info; public class ProcessAPIChanges {    public void detailedAPIInfo(ProcessHandle processHandle) {       Info processInfo = processHandle.info();       System.out.println("Detailed Process Info is Provided Below: ");       System.out.println("[Executable Name] " + processInfo.command().get());       System.out.println("[User Name] " + ... Read More

Semaphore in C#

Arjun Thakur
Updated on 01-Apr-2020 08:47:56

4K+ Views

The semaphore class lets you set a limit on the number of threads that have access to a critical section. The class is used to control access to a pool of resources. System.Threading.Semaphore is the namespace for Semaphore because it has all the methods and properties required to implement Semaphore.For using a semaphore in C#, you just need to instantiate an instance of a Semaphore object. It has minimum of two arguments −Reference−MSDNSr.No.Constructor & Description1Semaphore(Int32, Int32)Initializes a new instance of the Semaphore class, specifying the initial number of entries and the maximum number of concurrent entries.2Semaphore(Int32, Int32, String) −Initializes a ... Read More

Make Case-Insensitive Query in MongoDB

AmitDiwan
Updated on 01-Apr-2020 07:53:42

303 Views

For a case-insensitive query, use regex in MongoDB. Let us create a collection with documents −> db.demo314.insertOne({"Name":"Chris brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d742f8647eb59e562056") } > db.demo314.insertOne({"Name":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d743f8647eb59e562057") } > db.demo314.insertOne({"Name":"CHRIS BROWN"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d744f8647eb59e562058") } > db.demo314.insertOne({"Name":"DAVID MILLER"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d747f8647eb59e562059") } > db.demo314.insertOne({"Name":"chris brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d749f8647eb59e56205a") }Display all documents from a collection with the help of find() method −> db.demo314.find();This will produce the following output −{ "_id" ... Read More

Modifiers Not Allowed in Top-Level Declaration in JShell Java 9

raja
Updated on 01-Apr-2020 07:52:42

199 Views

JShell is an interactive tool for learning the Java language and prototyping Java code. It is a REPL (Read-Evaluate-Print-Loop) that evaluates declarations, statements, and expressions once entered and immediately prints the results in JShell. This tool runs from the command-line prompt.The modifiers like public, protected, private, static, and final have not allowed on top-level declarations and can be ignored with a warning. The keywords like synchronized, native, abstract, and default top-level methods have not allowed and can be errors.In the below code snippets, we have created both final and static variables. It prints out a warning message to the user that "Modifier 'final' or 'static' not permitted ... Read More

MongoDB $elemMatch to Match Document

AmitDiwan
Updated on 01-Apr-2020 07:51:20

329 Views

Let us create a collection with documents −> db.demo313.insertOne({"_id":100, "details":[{"Name":"Chris", "Age":24}]}); { "acknowledged" : true, "insertedId" : 100 } > db.demo313.insertOne({"_id":101, "details":[{"Name":"David", "Age":22}]}); { "acknowledged" : true, "insertedId" : 101 } > db.demo313.insertOne({"_id":102, "details":[{"Name":"Mike", "Age":25}]}); { "acknowledged" : true, "insertedId" : 102 }Display all documents from a collection with the help of find() method −> db.demo313.find();This will produce the following output −{ "_id" : 100, "details" : [ { "Name" : "Chris", "Age" : 24 } ] } { "_id" : 101, "details" : [ { "Name" : "David", "Age" : 22 } ] } { "_id" : 102, "details" ... Read More

Update Partial Number of Documents in MongoDB

AmitDiwan
Updated on 01-Apr-2020 07:47:24

203 Views

To update partial number of documents, set multi to true. Let us create a collection with documents −> db.demo312.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce16f8647eb59e56204a") } > db.demo312.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce19f8647eb59e56204b") } > db.demo312.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce1cf8647eb59e56204c") } > db.demo312.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce20f8647eb59e56204d") } > db.demo312.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce22f8647eb59e56204e") }Display all documents from a collection with the help of find() method −> db.demo312.find();This will produce the following output −{ "_id" : ObjectId("5e50ce16f8647eb59e56204a"), "FirstName" ... Read More

Get the Count of a Specific Value in MongoDB Quickly

AmitDiwan
Updated on 01-Apr-2020 07:45:50

289 Views

For faster queries, create an index. To get the count, use count(). Let us create a collection with documents −> db.demo311.ensureIndex({"Name":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo311.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cd01f8647eb59e562044") } > db.demo311.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cd05f8647eb59e562045") } > db.demo311.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cd06f8647eb59e562046") } > db.demo311.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cd0af8647eb59e562047") } > db.demo311.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cd0df8647eb59e562048") } > db.demo311.insertOne({"Name":"Chris"}); { ... Read More

MongoDB Query to Push Document into an Array

AmitDiwan
Updated on 01-Apr-2020 07:43:36

405 Views

To push document into an array, use $push along with update(). Let us create a collection with documents −>db.demo310.insertOne({"Name":"Chris", "details":[{"Id":101, "Subject":"MySQL"}, {"Id":102, "Subject":"MongoDB"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cabdf8647eb59e562043") }Display all documents from a collection with the help of find() method −> db.demo310.find();This will produce the following output −{    "_id" : ObjectId("5e50cabdf8647eb59e562043"), "Name" : "Chris", "details" : [       { "Id" : 101, "Subject" : "MySQL" }, { "Id" : 102, "Subject" : "MongoDB" }    ] }Following is the query to push document −> db.demo310.update({ _id:ObjectId("5e50cabdf8647eb59e562043")}, ...{ $push: {"details": { ...   "Id" ... Read More

Remove Entire Data from a MongoDB Collection

AmitDiwan
Updated on 01-Apr-2020 07:41:37

203 Views

To remove, use remove() in MongoDB. Let us create a collection with documents −> db.demo309.insertOne({ "details":[ { "Name":"Chris" }, { "Name":"David" }, { "Name":"Adam" } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4eb71af8647eb59e562040") } > db.demo309.insertOne({ "details":[ { "Name":"David" }, { "Name":"Mike" }, { "Name":"Bob" } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4eb7cbf8647eb59e562041") }Display all documents from a collection with the help of find() method −> db.demo309.find();This will produce the following output −{ "_id" : ObjectId("5e4eb71af8647eb59e562040"), "details" : [ { "Name" : "Chris" }, { "Name" : "David" }, { "Name" ... Read More

Advertisements