让我们创建一个包含文档的集合-
> 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" } ] }