【发布时间】:2015-11-10 11:28:34
【问题描述】:
我想通过 JSON 对象在数据库中进行搜索。
这是我的架构:
var patientSchema = new Schema({
firstname: String,
lastname: String,
age: String,
tel: String,
work: [workSchema],
});
我的 Angular js 请求,发送一个 JSON 对象,它将是:
{'firstname':'value', 'lastname':'value', 'age':'age','tel':tel}
有没有办法直接在 Patient Schema 中搜索匹配项? 所以如果 JSON 对象只包含名字值 .. 它会检查
在 MySQL 中我会这样做,
SELECT * FROM patients WHERE firstname LIKE '%'.json.firstname AND .....
我测试的是
var filter = "{'firstname': 'value', 'lastname': 'value', 'age':'value', 'tel': 'tel'}" //sent from angularjs client
var jsonFilter = JSON.parse(filter);
Patient.find().or([
{ 'firstname': { $regex: new RegExp(filter.firstname,'i') }},
{ 'lastname': { $regex: new RegExp(filter.lastname,'i') }},
{ 'age':{ $regex: new RegExp(filter.age,'i') }},
{ 'tel':{$regex: new RegExp(filter.tel,'i') }}]).exec(function(err, result) {
if ( err)
throw err;
res.json(result);
});
这工作正常,但如果属性为空,则应填充所有数据,它将返回未定义,这不会让我得到正确的数据。因为 Angular JS 只发送 $scope.data。
有没有办法通过我的 JSON 对象获取所有数据,而不是重写所有 JSON 字段,因为我需要在这个项目中制作更大的过滤器?
【问题讨论】:
标签: angularjs node.js mongodb express mongoose