mongodb shell是使用javascript来进行操作的,因此模糊查询也采用了js的正则表达式。
没啥说的,mark一下。
app.js
var db = require('mongoskin').db('localhost:27017/rockband', {
safe : false
});
var bands = db.collection('bands');
bands.find({
$query : {
members : /o{1}/g // 模糊查询——采用正则表达式
}
}).toArray(function(err, result) {
console.log(result);
})
bands.find({
$query : {
members : {
$regex : /tt{1}/i
}
}
}).toArray(function(err, result) {
console.log(result);
});
package.json
{
"name": "hellomongo",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"underscore": ">=1.4.4",
"mongodb":"*",
"mongoskin":"*"
}
}