gpt4 book ai didi

javascript - $regex 在 MongoDB 中不起作用

转载 作者:可可西里 更新时间:2023-11-01 10:40:14 25 4
gpt4 key购买 nike

我知道有很多与 $regex 相关的答案,但我已经尝试了几个小时,但我无法让它工作。

我想要的是在courseName中找到那些包含字母“D”的courses。我究竟做错了什么?请帮我!

我有以下文件:

{
"firstName": "Sarah",
"lastName": "Jepsen",
"age": 27,
"courses": [
{
"courseName": "Web-Development",
"teachers": [
{
"firstName": "Santiago",
"lastName": "Donoso"
}
]
},

{
"courseName": "Databases",
"teachers": [
{
"firstName": "Dany",
"lastName": "Kallas"
},
{
"firstName": "Rune",
"lastName": "Lyng"
}
]
},
{
"courseName": "Interface-Design",
"teachers": [
{
"firstName": "Roxana",
"lastName": "Stolniceanu"
}
]
}
]
}

这是我的查找查询:

student.findCourses = (fcallback) => {
global.db.collection('students').find({ "courses.courseName": { $regex: "/D/" }, _id: false }).toArray((err, result) => {

if (err) {
var jError = { "status": "error", "message": "ERROR -> student.js -> 001" }
console.log(jError)
return fcallback(true, jError)
}
var jOk = { "status": "ok", "message": "student.js -> found -> 000" }
console.log(jOk)
console.log(JSON.stringify(result))
return fcallback(false, jOk)
})
}

最佳答案

要获得与您的正则表达式匹配的所有类(class),您必须手动过滤它们。考虑你的情况,使用另一个 courses 数组项:

{
"courseName": "Unmatching", // no letter "D"
"teachers": [
{
"firstName": "Dany",
"lastName": "Kallas"
}
]
}

您的查询无论如何都会返回它,因为学生文档中的其他类(class)与您的正则表达式匹配。

你可以做的是:

  1. 查询 (.find()) 匹配类(class)名称的文档
  2. 手动过滤 - 遍历每个项目以检查其名称是否与正则表达式匹配

示例数据:

// 1
{
"_id" : ObjectId("5a019896f89c24926108e2bf"),
"firstName" : "Sarah",
"lastName" : "Jepsen",
"age" : 27,
"courses" : [
{
"courseName" : "Lorem",
"teachers" : []
},
{
"courseName" : "Ipsum",
"teachers" : []
},
{
"courseName" : "Dolor",
"teachers" : []
},
{
"courseName" : "Sit",
"teachers" : []
},
{
"courseName" : "Dolor 2",
"teachers" : []
}
]
},
// 2
{
"_id" : ObjectId("5a019896f89c24926108e2be"),
"firstName" : "John",
"lastName" : "Smith",
"age" : 22,
"courses" : [
{
"courseName" : "Dioptry",
"teachers" : []
},
{
"courseName" : "Dino",
"teachers" : []
},
{
"courseName" : "A",
"teachers" : []
},
{
"courseName" : "B",
"teachers" : []
},
{
"courseName" : "C",
"teachers" : []
}
]
}

查询&过滤代码:

student.findCourses = (fcallback) => {

var regexp = new RegExp('D');

global.db.collection('students').find({ "courses.courseName": { $regex: regexp } }, { _id: 0, courses: 1 } }).toArray((err, result) => {

// here, you'll receive:
// [{ courses: [] }, { courses: [] }]
// with all the courses inside
// no matter if their names match the given regex
// as it's enough that one of them does
// to return the whole document

// filtering:
var filtered = result.map(student => {
student.courses = student.courses.filter(course => regexp.test(course.courseName));
return student;
});

// now, your filtered has exactly the same format
// [{ courses: [] }, { courses: [] }]
// but only with courses matching the regexp inside it

console.log(JSON.stringify(filtered);

// what you can do to get flat courses array is:
var flat = filtered.reduce((p, c) => p.concat(c.courses), [])

console.log(JSON.stringify(flat));


if (err) {
var jError = { "status": "error", "message": "ERROR -> student.js -> 001" }
console.log(jError)
return fcallback(true, jError)
}
var jOk = { "status": "ok", "message": "student.js -> found -> 000" }
console.log(jOk)
console.log(JSON.stringify(result))
return fcallback(false, jOk)
})
}

关于javascript - $regex 在 MongoDB 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47162993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com