- 精确查询
当{‘name’:’未来警察’}
时,精确匹配到一条记录。 - 模糊查询
{‘name’:/未来/}
,匹配到了多条记录。
代码:
- movieRouter.js
var express = require('express'); var router = express.Router(); var movieDao = require('./Movie'); //模糊查询 router.get('/add', function (req, res, next) { var query = {}; if (req.query.m2) { //可以把m2字符串变成正则表达式 //如果m2是'未' //new之后变成了/未/ query.name = new RegExp(req.query.m2); console.log(query.name); } movieDao.findByName(query.name, function (err, list) { if (err) { console.log(err); return; } console.log(list); }) }); module.exports = router;
- Movie.js
const mongodb = require('./mongodb'); const Schema = mongodb.mongoose.Schema; const MovieSchema = new Schema({ name: String, alias: [String], publish: Date, create_date: { type: Date, default: Date.now }, images: { coverSmall: String, coverBig: String, }, source: [{ source: String, link: String, swfLink: String, quality: String, version: String, lang: String, subtitle: String, create_date: { type: Date, default: Date.now } }] }); const Movie = mongodb.mongoose.model('Movie', MovieSchema); const MovieDAO = function () { }; MovieDAO.prototype.save = function (obj, callback) { var instance = new Movie(obj); instance.save(function (err) { callback(err); }); }; MovieDAO.prototype.findByName = function (name, callback) { Movie.findOne({ name: name }, function (err, obj) { callback(err, obj); }); } module.exports = new MovieDAO();