我正在spring data mongodb
中寻找看起来像这样的请求:
?#{ [2] == null ? { $where : 'true'} : { 'vehicle.immatriculation' : {'$regex' : {'$replaceAll':[2],'-',''}, $options : 'x'} } }"
是否可以提出此类请求?我被困住了,因为我找不到替换SQL
版本
在SQL
中
SELECT colonne1, colonne2, REPLACE(colonne3, '-', '')
FROM table
该功能还不存在。 SERVER-32314是将$replaceOne
和$replaceAll
添加为聚合运算符的功能请求。看来此请求可能在下一个版本中,但并未使MongoDB 4.2受益匪浅]
该服务器票证还提供了一种解决方法。我根据您的结构调整了该示例,但是我对Spring不够熟悉,无法为您提供该语法。它在mongo shell javascript中,也许其他人可以将其翻译为Java。
db.collection.aggregate([{$project:{
_id:0,
colonne1:1,
colonne2:1,
colonne3:{$let:{
vars:{split:{$split:["$colonne3","-"]}},
in:{$reduce:{
input:{$slice:["$$split",1,{$size:"$$split"}]},
initialValue:{$arrayElemAt:["$$split",0]},
in:{$concat:["$$value","$$this"]}
}}
}}
}}])