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中檢查一下語法。