- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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" } ] }
Advertisements