您可以使用$unset。首先让我们创建一个包含文档的集合-
> db.demo20.insertOne( ... { ... ... "ListOfEmployee" : [ ... { ... "EmployeeName1" : "John" ... }, ... { ... "EmployeeName2" : "Carol" ... } ... ], ... "EmployeeName2" : [] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e138c3555d0fc6657d21f12") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.demo20.find();
这将产生以下输出-
{ "_id" : ObjectId("5e138c3555d0fc6657d21f12"), "ListOfEmployee" : [ { "EmployeeName1" : "John" }, { "EmployeeName2" : "Carol" } ], "EmployeeName2" : [ ] }
以下是删除文档中特定数据的查询-
> db.demo20.update({ "EmployeeName2": { "$exists": 1 }},{ "$unset": { "EmployeeName2": "" } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.demo20.find();
这将产生以下输出-
{ "_id" : ObjectId("5e138c3555d0fc6657d21f12"), "ListOfEmployee" : [ { "EmployeeName1" : "John" }, { "EmployeeName2" : "Carol" } ] }