AmitDiwan has Published 10744 Articles

Updating nested document in MongoDB

AmitDiwan

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 ... Read More

Get the number of open connections in MongoDB?

AmitDiwan

AmitDiwan

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

420 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" : ... Read More

MongoDB query to set a sub item in an array?

AmitDiwan

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', ... Read More

How can I update child objects in MongoDB database?

AmitDiwan

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" ... Read More

Shutdown MongoDB with auth enabled?

AmitDiwan

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 ... Read More

How to delete particular data in a document in MongoDB?

AmitDiwan

AmitDiwan

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

200 Views

You can use $unset. Let us first create a collection with documents −> db.demo20.insertOne( ...    { ... ...       "ListOfEmployee" : [ ...          { ...             "EmployeeName1" : "John" ...          }, ...     ... Read More

Using a regex with text search in MongoDB

AmitDiwan

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,   ... Read More

Convert a field to an array using update operation in MongoDB

AmitDiwan

AmitDiwan

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

237 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 ... Read More

Filter documents in MongoDB if all keys exist as fields?

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:26:33

307 Views

For this, use $all, that would find documents containing all of the elements in an array like "keys". Let us first create a collection with documents −> db.demo17.insertOne({"ListOfSubject":["MySQL", "MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e13847255d0fc6657d21f0a") } > db.demo17.insertOne({"ListOfSubject":["C", "Python", "Java"]}); {    "acknowledged" : true,   ... Read More

Perform group and distinct together in a single MongoDB query

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 14:25:02

491 Views

For this, simply use MongoDB $group. Let us first create a collection with documents −> db.demo16.insertOne({ ...    "StudentName" : "Chris", ...    "StudentSection" : "A", ...    "StudentAge" : 23, ...    "StudentMarks" : 47 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e13827455d0fc6657d21f07") } ... Read More

Advertisements