在包含伺服器記錄的 MongoDB 集合中將伺服器的狀態設定為不活動?
讓我們建立一個包含文件的集合 -
> 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") }
在集合中使用 find() 方法顯示所有文件 -
> db.demo707.find();
這將生成以下輸出 -
{ "_id" : ObjectId("5ea6f852551299a9f98c93c8"), "id" : 101, "serverInformation" : [ { "IP" : "192.56.34.3", "Status" : "Active" }, { "IP" : "192.56.36.4", "Status" : "Inactive" } ] }
以下是將活動伺服器設定為非活動狀態的查詢 -
>db.demo707.update({"serverInformation.IP":"192.56.34.3"},{$set:{"serverInformation.$.Status":"Inactive"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
在集合中使用 find() 方法顯示所有文件 -
> db.demo707.find();
這將生成以下輸出 -
{ "_id" : ObjectId("5ea6f852551299a9f98c93c8"), "id" : 101, "serverInformation" : [ { "IP" : "192.56.34.3", "Status" : "Inactive" }, { "IP" : "192.56.36.4", "Status" : "Inactive" } ] }
廣告