इसे आजमाएं:
let keyword = new RegExp("Hi", "i")
db.myCollection.aggregate([
{
$addFields: {
num_of_occ: {
$size: {
$filter: {
input: { $split: ["$description", " "] },
as: "text",
cond: {
$regexMatch: { input: "$$text", regex: keyword }
}
}
}
}
}
}
]);
आउटपुट:
[
{
"_id" : ObjectId("6033a3b0406aaa04445434a1"),
"name" : "Dheemanth",
"description" : "Everything has hI in it.",
"num_of_occ" : 2
},
{
"_id" : ObjectId("6033a3b0406aaa04445434a2"),
"name" : "Heath",
"description" : "shushi ends with Hi.",
"num_of_occ" : 2
},
{
"_id" : ObjectId("6033a3b0406aaa04445434a3"),
"name" : "Abdelmlak",
"description" : "Hi i am Abdelmlak.",
"num_of_occ" : 1
},
{
"_id" : ObjectId("6033a3b0406aaa04445434a4"),
"name" : "Alex",
"description" : "missing keyword",
"num_of_occ" : 0
}
]