Set server status to inactive in a MongoDB collection with server records?



Let us create a collection with documents −

> db.demo707.insertOne( ...    { ...       id:101, ...       "serverInformation": ...       [ ...          { ...             "IP":"192.56.34.3", ...             "Status":"Active" ...          }, ...          { ...             "IP":"192.56.36.4", ...             "Status":"Inactive" ...          } ...       ] ...    } ... ); {    "acknowledged" : true,       "insertedId" : ObjectId("5ea6f852551299a9f98c93c8") }

Display all documents from a collection with the help of find() method −

> db.demo707.find();

This will produce the following output −

{ "_id" : ObjectId("5ea6f852551299a9f98c93c8"), "id" : 101, "serverInformation" : [ { "IP" : "192.56.34.3", "Status" : "Active" }, { "IP" : "192.56.36.4", "Status" : "Inactive" } ] }

Following is the query to set active server to inactive status −

>db.demo707.update({"serverInformation.IP":"192.56.34.3"},{$set:{"serverInformation.$.Status":"Inactive"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Display all documents from a collection with the help of find() method −

> db.demo707.find();

This will produce the following output −

{ "_id" : ObjectId("5ea6f852551299a9f98c93c8"), "id" : 101, "serverInformation" : [ { "IP" : "192.56.34.3", "Status" : "Inactive" }, { "IP" : "192.56.36.4", "Status" : "Inactive" } ] }
Updated on: 2020-05-14T09:55:33+05:30

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements