如何使用Springboot在MongoDB中的字符串中计数特殊字符的发生?
在Mongo系列中,大约有120K文档。对于每个文档,我必须检查消息字段中的特殊字符'@'的发生。
以下是JSON格式的文档
[{
"_id":356,
"message":"ty@78i@gh8"
}]
,如果它包含超过2 @。我将执行一些Java功能。 我尝试了以下解决方案 -
db.test.aggregate( [
{
$project: {
_id: 0,
message: 1,
count: {
$subtract: [
{ $size: { $split: [ "$message", "@" ] } }, 1
]
}
}
}
] )
Java Equavilent代码 -
DBObject project = new BasicDBObject("_id",0).append(message, 1)
.append(count, new BasicDBObject("$submit", new BasicDBObject("size",
new BasicDBObject("$split", "$messgae").append(@))));
Java等效代码实际上是不完整的,因为我不知道该如何汇总,然后为每个文档进行遍历。
In a mongo collection there are around 120k documents. For each document I have to check how many occurrences of special character '@' is in message field.
Below is the document in json format
[{
"_id":356,
"message":"ty@78i@gh8"
}]
If it contains more than 2 @. I will perform some java function.
I tried the below solution --
db.test.aggregate( [
{
$project: {
_id: 0,
message: 1,
count: {
$subtract: [
{ $size: { $split: [ "$message", "@" ] } }, 1
]
}
}
}
] )
Java equavilent code --
DBObject project = new BasicDBObject("_id",0).append(message, 1)
.append(count, new BasicDBObject("$submit", new BasicDBObject("size",
new BasicDBObject("$split", "$messgae").append(@))));
Java equivalent code is actually incomplete because I don't know how can I aggregate and then traverse for each document.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论