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

MongoDBDatabaseBig Data Analytics

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" } ] }
raja
Published on 14-May-2020 13:17:49
Advertisements