您的位置:首页 > 数据库 > Mongodb

Mongodb的模糊查询——备忘

2013-03-27 00:23 190 查看
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":"*"
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息