在包含服务器记录的 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" } ] }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP