mongo-查询(4)——正则表达式

mongo的find中,还可以使用正则表达式。

> db.tianyc02.find({name:/xTtt/i})
{ "_id" : ObjectId("50ea6eba12729d90ce6e3423"), "name" : "xttt", "age" : 111 }
{ "_id" : ObjectId("50ea6eba12729d90ce6e3424"), "name" : "xttt", "age" : 222 }

> db.tianyc02.find({name:/xTt$/i})
{ "_id" : ObjectId("50ea6b6f12729d90ce6e341b"), "name" : "xtt", "age" : 11, "sex" : null }
{ "_id" : ObjectId("50ea6b7312729d90ce6e341c"), "name" : "xtt", "age" : 22, "sex" : "m" }
>

mongo使用perl兼容的正则表达式库来匹配正则表达式。建议在使用正则作为mongo的查询条件前,先在js shell中检查一下语法。

posted @ 2013-02-04 11:41  醇酒醉影  阅读(5331)  评论(0编辑  收藏  举报