keyword = '' post = collection.find({ 'key': '/^' + keyword })
如上面的代碼所示,當(dāng)keyword包含一些正則符號(hào),比如$
之類(lèi)時(shí),查詢結(jié)果就不準(zhǔn)確了。
不知道m(xù)ongodb有沒(méi)有內(nèi)置轉(zhuǎn)義的方法,還是說(shuō)必須要外部對(duì)keyword進(jìn)行轉(zhuǎn)義?
小伙看你根骨奇佳,潛力無(wú)限,來(lái)學(xué)PHP伐。
Mongodb’s regular expression query has two formats
One is {key:{$regex:".*"}}
The other is {key:/.*/}
For the first escape use \
The second use