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.