使用正则表达式的 mongoDB 更新语句 [英] mongoDB update statement using regex
问题描述
我在 mongoDB 中有以下内容:
I have the following in mongoDB:
{
"_id" : ObjectId("552f4bf5344fbaee0f62ccef"),
"name" : "testcase2",
"steps" : [
{
"spec1" : "postPet"
},
{
"spec2" : "putPet"
},
{
"spec2" : "getPetsHistory"
}
]
}
我想从步骤数组中提取一个元素.我只能使用以下值:testcase2"、postPet".
I want to pull an element out of the steps array. I can only use the following values: "testcase2", "postPet".
我在更新查询中使用了正则表达式,如以下链接所述:https://gist.github.com/gatesvp/1021164
I used regex in the update query as discussed in the following link: https://gist.github.com/gatesvp/1021164
我的查询是:
db.testcaseCollection.update({"name" : "testcase3"}, {$pull : {"steps" : {$regex: 'postPet'}}})
结果是:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
谁能帮助我解决我做错的事情?
Can anyone help with what I am doing wrong?
推荐答案
您正在尝试根据正则表达式从数组中提取字符串.问题是你的数组由地图/对象/任何东西组成,所以你不能拉任何东西也就不足为奇了.这就是为什么您会看到 1 个 matched
和 0 个 modifying
.
You are trying to pull a string from an array based on the regex. The problem is that your array consists of maps/objects/whatever, so not surprisingly you can not pull anything. This is why you see 1 matched
and 0 modifies
.
db.testcaseCollection.update({
"name" : "testcase2"
}, {
$pull : {
steps: { spec1: {$regex: 'postPet' }}
}
})
查看文档,了解如何使用 $pull
Take a look at documentation as to how you can use $pull
这篇关于使用正则表达式的 mongoDB 更新语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!