To simplify the previous day’s entry, we have text_without_smiley with the result of the $concat operation, which combines the first and second substrings from the previous day’s journal.
// test data
db.getCollection("unicode_demo").insertMany([
{ label: "with_emoji", text: "Hi☺!" },
{ label: "multi_byte_chars", text: "Héllo☺!" },
]);
db.getCollection("unicode_demo").aggregate([
{
$project: {
label: 1,
text: 1,
text_without_smiley: {
$let: {
vars: {
smileyIndex: { $indexOfCP: ["$text", "☺"] },
textLen: { $strLenCP: "$text" },
},
in: {
$concat: [
{ $substrCP: ["$text", 0, "$$smileyIndex"] },
{
$substrCP: [
"$text",
{ $add: ["$$smileyIndex", 1] },
{ $subtract: ["$$textLen", { $add: ["$$smileyIndex", 1] }] },
],
},
],
},
},
},
},
},
]);
Output:
[
{
_id: ObjectId('69b6987355ed455452544ca7'),
label: 'with_emoji',
text: 'Hi☺!',
text_without_smiley: 'Hi!'
},
{
_id: ObjectId('69b6987355ed455452544ca8'),
label: 'multi_byte_chars',
text: 'Héllo☺!',
text_without_smiley: 'Héllo!'
}
]