要从数组中删除项目,请在MongoDB中使用$pull。让我们创建一个包含文档的集合-
> db.demo224.insertOne({"ListOfTechnology":["Spring","Hibernate","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee6d103d395bdc2134733") } > db.demo224.insertOne({"ListOfTechnology":["Groovy"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee6ec03d395bdc2134734") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo224.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3ee6d103d395bdc2134733"), "ListOfTechnology" : [ "Spring", "Hibernate", "Java" ] } { "_id" : ObjectId("5e3ee6ec03d395bdc2134734"), "ListOfTechnology" : [ "Groovy" ] }
以下是从MongoDB中的数组中删除项目的查询-
>db.demo224.update({_id:ObjectId("5e3ee6d103d395bdc2134733")},{$pull:{"ListOfTechnology":"Java"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
在find()
方法的帮助下显示集合中的所有文档-
> db.demo224.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3ee6d103d395bdc2134733"), "ListOfTechnology" : [ "Spring", "Hibernate" ] } { "_id" : ObjectId("5e3ee6ec03d395bdc2134734"), "ListOfTechnology" : [ "Groovy" ] }