Updating Nested Document in MongoDB

AmitDiwan
Updated on 02-Apr-2020 05:56:30

329 Views

To update the nested document, use $set. Let us create a collection with documents −> db.demo315.insertOne({ _id :101, ...  details: [ ...     {Name: 'Chris', subjects: [{id:1001, SubjectName:"MySQL"}]} ...  ] ... } ...) { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo315.find().pretty();This will produce the following output −{    "_id" : 101,    "details" : [       {          "Name" : "Chris",          "subjects" : [             {             ... Read More

Get the Number of Open Connections in MongoDB

AmitDiwan
Updated on 02-Apr-2020 05:37:14

421 Views

To get the number of open connections, use serverStatus() in MongoDB. Following is the query −> db.serverStatus();This will produce the following output −{    "host" : "DESKTOP-QN2RB3H",    "version" : "4.0.5",    "process" : "mongod",    "pid" : NumberLong(10540),    "uptime" : 74156,    "uptimeMillis" : NumberLong(74156688),    "uptimeEstimate" : NumberLong(74156),    "localTime" : ISODate("2020-01-07T15:09:18.366Z"),    "asserts" : {       "regular" : 0,       "warning" : 0,       "msg" : 0,       "user" : 1,       "rollovers" : 0    },    "connections" : {       "current" : ... Read More

Create Multi-Release JAR (MRJAR) Using JAR Tool in Java 9

raja
Updated on 01-Apr-2020 16:29:09

252 Views

In Java 9, a new feature "multi-release jar format" has been introduced where jar format enhanced with different versions of Java class or resources that can be maintained and used as per the platform. A jar command can be used to create a multi-release jar that contains two versions of the same class compiled for both Java 8 and Java 9 versions with a warning message, telling that both classes are identical.C:\Users\User\tutorialspoint>jar --create --file MR.jar -C sampleproject-base demo --release 9 -C sampleproject-9 demo Warning: entry META-INF/versions/9/demo/SampleClass.class contains a class thatis identical to an entry already in the jarThe " --release 9" option can tell jar ... Read More

Check if a String is Palindrome in JShell with Java 9

raja
Updated on 01-Apr-2020 15:47:43

153 Views

JShell is the first REPL(Read-Evaluate-Print-Loop) interactive tool that has been introduced as a part of Java 9. It evaluates declarations, statements, and expressions as entered and immediately shows the results, and it runs from the command-line prompt.Palindrome string is a string where it remains the same when reversed or word spelled the same way in both forward and backward directions.In the below example, we can able to check whether the given string is palindrome or not in the JShell tool.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> String str="LEVEL"; str ==> "LEVEL" jshell> ... Read More

Set a Sub-item in an Array with MongoDB Query

AmitDiwan
Updated on 01-Apr-2020 14:46:08

273 Views

You can use positional $ operator. Let us first create a collection with documents −> db.demo22.insertOne( ...    { ...       ProductId:101, ... ...       ProductDetails: ...       [ ...          { ...             ProductFirstPrice: '35', ...             ProductSecondPrice: '75' ...          }, ...          { ...             ProductFirstPrice: '', ...             ProductSecondPrice:'' ...          }, ...          { ... Read More

Update Child Objects in MongoDB Database

AmitDiwan
Updated on 01-Apr-2020 14:42:13

315 Views

To update child objects, use $set in MongoDB. Let us first create a collection with documents −>db.demo21.insertOne({"StudentId":"STU-101", "StudentDetails":{"StudentName":"Chris", "StudentAge":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14be8922d07d3b95082e6f") }Display all documents from a collection with the help of find() method −> db.demo21.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e14be8922d07d3b95082e6f"),    "StudentId" : "STU-101",    "StudentDetails" : {       "StudentName" : "Chris",       "StudentAge" : 21    } }Following is the query to update child objects in MongoDB −> db.demo21.update({"StudentId":'STU-101'}, {$set:{'StudentDetails.StudentName':'Robert'}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents ... Read More

Shutdown MongoDB with Auth Enabled

AmitDiwan
Updated on 01-Apr-2020 14:40:23

139 Views

To shutdown MongoDB, you need to use shutdownServer() as in the below syntax −db.shutdownServer();First you need to switch to admin as shown below −use admin;Here, we switched to admin −> use admin; switched to db adminFollowing is the query to shutdown server −> db.shutdownServer();This will produce the following output −server should be down... 2020-01-07T22:40:31.295+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed 2020-01-07T22:40:32.326+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed

Delete Particular Data in a Document in MongoDB

AmitDiwan
Updated on 01-Apr-2020 14:31:47

201 Views

You can use $unset. Let us first create a collection with documents −> db.demo20.insertOne( ...    { ... ...       "ListOfEmployee" : [ ...          { ...             "EmployeeName1" : "John" ...          }, ...          { ...             "EmployeeName2" : "Carol" ...          } ...       ], ...       "EmployeeName2" : [] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e138c3555d0fc6657d21f12") }Following is the query to ... Read More

Using a Regex with Text Search in MongoDB

AmitDiwan
Updated on 01-Apr-2020 14:30:21

538 Views

To filter records using Regular Expression in MongoDB, use $regex. Let us first create a collection with documents −> db.demo19.insertOne({"Values":"4321GH"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1389b955d0fc6657d21f0f") } > db.demo19.insertOne({"Values":"12321_Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1389c755d0fc6657d21f10") } > db.demo19.insertOne({"Values":"8765Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1389d355d0fc6657d21f11") }Following is the query to display all documents from a collection with the help of find() method −> db.demo19.find();This will produce the following output −{ "_id" : ObjectId("5e1389b955d0fc6657d21f0f"), "Values" : "4321GH" } { "_id" : ObjectId("5e1389c755d0fc6657d21f10"), "Values" : "12321_Carol" } { "_id" : ObjectId("5e1389d355d0fc6657d21f11"), "Values" : "8765Mike" ... Read More

Convert a Field to an Array Using Update Operation in MongoDB

AmitDiwan
Updated on 01-Apr-2020 14:28:48

238 Views

To convert a field to an array, use UPDATE operation inside forEach(). Let us first create a collection with documents −> db.demo18.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1387fc55d0fc6657d21f0e") }Following is the query to display all documents from a collection with the help of find() method −> db.demo18.find();This will produce the following output −{ "_id" : ObjectId("5e1387fc55d0fc6657d21f0e"), "StudentName" : "John" }Here is the query to convert a field to an array using update operation −> db.demo18.find().forEach(function(myDocument) { ...    db.demo18.update( ...       { _id: myDocument._id }, ...       { "$set": { "StudentName": [myDocument.StudentName] } ... Read More

Advertisements