mongodb - MongoDB 控制台 - 使用正则表达式在基于嵌套对象的数组中查找和拉取对象
问题描述
我遇到了一个问题,我需要根据在字段值中查找“x”字符的正则表达式删除数据库中的一些条目。这是我的数据结构:
{
"_id" : ObjectId("5e0effac04d45483d97cb4c3"),
"formId" : ObjectId("5d076f7db166bcae2743554a"),
"revisionNo" : 0,
"customerUserId" : ObjectId("5d0889fde55fbc38509e5825"),
"formElements" : [
{
"formElementId" : ObjectId("5d1a047cae7a711848238c26"),
"type" : "text",
"label" : "Intersection number",
"value" : null,
"dynamicTypeData" : {
"required" : false,
"value" : "TK47ax"
}
},
{
"formElementId" : ObjectId("5d1a058cae7a711848238c27"),
"type" : "select",
"label" : "Municipality",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00ec9a9001eb637c8466f4"),
"type" : "select",
"label" : "Signtype for setup",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00ee749001eb637c8466fc"),
"type" : "select",
"label" : "Sign size",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00eced9001eb637c8466f5"),
"type" : "select",
"label" : "Route number",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00eeba9001eb637c8466fe"),
"type" : "select",
"label" : "Stand",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00ee849001eb637c8466fd"),
"type" : "select",
"label" : "Foundation",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00ed0f9001eb637c8466f6"),
"type" : "textarea",
"label" : "Remarks regaring the new signs placement",
"value" : null,
"dynamicTypeData" : {
"value" : null,
"required" : false
}
}
]
}
所以我想要找到的是每个元素都与label: "Intersection number"
包含x
其dynamicTypeData
对象中的字符的值相匹配。我知道我可以在控制台中执行以下操作:db.getCollection('formRegistrations').find({formId: ObjectId("5d0889fde55fbc38509e5825")}) but how to get all the way down to the
dynamicTypeData` 对象并与其中的值匹配,我不知道。请帮助:) 感谢阅读
编辑
虽然给定的答案是正确的,但我仍然只想找到具有这些参数的给定注册,因为我必须在删除之前手动查看数据,因为这是在生产服务器上完成的。因此,根据下面给出的答案,我制定了自己的脚本:
db.formRegistrations.find(
{
formId: ObjectId("5d076f7db166bcae2743554a"),
"formElements.0.label": "Intersection number",
"formElements.0.dynamicTypeData.value": {
$regex: /.*x/
}
}
)
希望这对我以外的人有所帮助
解决方案
要删除formElements
匹配的特定label
和dynamicTypeData
(子元素)值,这应该有效:
db.formRegistrations.update({ _id: <the id> }, {
$pull: {
formElements: {
"label": "Intersection number",
"dynamicTypeData.value": {
$regex: /^.*?x.*$/,
$options: "i"
}
}
}
})
推荐阅读
- python - 查询 Django 中所有类型的每个唯一类型的第 n 个最新条目
- mysql - Flutter 可扩展列表视图
- android - 如何从隐式意图中删除共享和打印选项?
- java - 为什么我的程序仅在一个循环后终止,而没有第二次提示?
- firebase - 从 PWA 服务工作者调用 Firebase 可调用函数以响应对推送通知的点击
- go - 使用 golang-ci lint 错误获取 corp 包
- amazon-eks - EKS,如何创建 2 个 LoadBalancer 服务共享一个静态 IP
- reactjs - React Router 没有从像 path="/products?category=:category" 这样的请求中获取数据
- c# - 发布后 Blazor Wasm 托管客户端(控制台)异常
- python - Scrapy run 爬行一个接一个