請問 MongoDB 有無類似 MySQL instr 功能?
-
Output : mysql> SELECT INSTR('myteststring','st'); +----------------------------+ | INSTR('myteststring','st') | +----------------------------+ | 5 | +----------------------------+ 1 row in set (0.03 sec)
-
3.2以前沒有這個功能!
-
所以 3.2+ 有這功能?
-
db.eval( function(a,b){
return {"result": a.indexOf(b)+1}
}, "myteststring", "st");
-
在 3.4 以後有提供 $indexOfBytes
https://docs.mongodb.com/manual/reference/operator/aggregation/indexOfBytes/
-
@yea 我個人覺得$indexOfCP (aggregation),或許比較準一點
本帖下載内容已隐藏,请登入以查看隐藏内容!
-
以官網的範例來看,$indexOfBytes 才是對的
找 "foo" 的例子
{ "_id" : 2, "item" : "fóofoo" } => 使用 $indexOfBytes 為 4,使用 $indexOfCP 為 3
驗證 MYSQL 語法 SELECT INSTR('fóofoo','foo'); 為 4