$ regex在MongoDB中不起作用
我知道有很多与$regex
有关的答案,但是现在我已经试了好几个小时了,而且我不能得到这个工作。
我想要的是在courseName
find包含字母“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) }) }
要获得所有符合你的正则expression式的课程,你必须手动过滤它们。 考虑你的情况,与另一个courses
数组项目:
{ "courseName": "Unmatching", // no letter "D" "teachers": [ { "firstName": "Dany", "lastName": "Kallas" } ] }
您的查询将返回它,因为学生文档中的其他课程匹配您的正则expression式。
你可以做什么,是:
- 查询(
.find()
)匹配课程名称的文档 - 手动过滤它们 – 循环遍历每个项目检查,如果其名称匹配正则expression式
示例数据:
// 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) }) }
你尝试过吗?
global.db.collection('students') .find({ "courses.courseName": { $regex: new RegExp('D') }, _id: false })
我认为MongoDB客户端可以理解RegExp或string字面模式,但我认为你可能在这里混合了两种不同的模式。 如果你把"/D/"
你正在寻找一个课程,其中包含字面/D/
在名称,而不仅仅是D
相关: MongoDB正则expression式search – 从使用JavaScript驱动程序和NodeJS开始
编辑:
什么是_id: false
为? 如果您尝试进行投影(并删除_id
字段),则您的语法不正确。 { _id: false }
应作为第二个parameter passing给.find()
。 您当前的查询与正则expression式匹配,并检查_id
等于false
。
global.db.collection('students') .find({ "courses.courseName": { $regex: /D/ } }, { _id: false })