如何从 MongoDB 文档中的数组中移除一个字符串?
可以使用 $pull 运算符从数组中移除一个字符串。我们首先使用文档创建集合 -
> db.removeAStringDemo.insertOne({"Score":[45,67,89,"John",98,99,67]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cda5224b50a6c6dd317adbd")
}下面是使用 find() 方法显示存储中所有文档的查询 -
> db.removeAStringDemo.find().pretty();
这将产生以下输出 -
{
"_id" : ObjectId("5cda5224b50a6c6dd317adbd"),
"Score" : [
45,
67,
89,
"John",
98,
99,
67
]
}以下是用于移除 MongoDB 文档中数组中的字符串的查询。要移除的字符串是“John” -
> db.removeAStringDemo.update( { _id :ObjectId("5cda5224b50a6c6dd317adbd") }, { $pull: { "Score":"John" }});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })让我们再次检查一下该文档 -
> db.removeAStringDemo.find().pretty();
这将产生以下输出 -
{
"_id" : ObjectId("5cda5224b50a6c6dd317adbd"),
"Score" : [
45,
67,
89,
98,
99,
67
]
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP