模糊查询
记录如下:
{ "_id" : ObjectId("5c3d486d24aa9a000526367b"), "name" : "fuc" } { "_id" : ObjectId("5c3d486d24aa9a000526367b"), "name" : "fuc_1" } { "_id" : ObjectId("5c3d486d24aa9a001526367b"), "name" : "fuc_2" } { "_id" : ObjectId("5c469cd27353b40005bcb67b"), "name" : "map (2)_2" } { "_id" : ObjectId("5c469cd27353b40005bcb67c"), "name" : "map (2)_3" } { "_id" : ObjectId("5c469cd37353b40005bcb67d"), "name" : "map (2)_4" } { "_id" : ObjectId("5c469cd37353b40005bcb67e"), "name" : "map (2)_5" }
查询name以fuc开头的:
db.getCollection('maps').find({"name" :{$regex: /fuc/}})
结果如下:
{ "_id" : ObjectId("5c3d486d24aa9a000526367b"), "name" : "fuc" } { "_id" : ObjectId("5c3d486d24aa9a000526367b"), "name" : "fuc_1" } { "_id" : ObjectId("5c3d486d24aa9a001526367b"), "name" : "fuc_2" }
模糊查询中带有括号的,重点是将括号用反斜杠""进行转义:
db.getCollection('maps').find({"name" :{$regex: /map (2)/}})
这样就能模糊查询出带有括号的记录了:
{ "_id" : ObjectId("5c469cd27353b40005bcb67b"), "name" : "map (2)_2" } { "_id" : ObjectId("5c469cd27353b40005bcb67c"), "name" : "map (2)_3" } { "_id" : ObjectId("5c469cd37353b40005bcb67d"), "name" : "map (2)_4" } { "_id" : ObjectId("5c469cd37353b40005bcb67e"), "name" : "map (2)_5" }