如何从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
]
}热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西