[
{lecture : "427#Math"},
{lecture : "217#Science"},
{lecture : "7#History"},
{lecture : "12#Music"}
]
Assume I have the database structure above. I want to return only the lecture code. What have I done so far?
db.collection.aggregate([
{$project: {"lecture": {$split: ["$lecture" , "#"]}}}
])
But this returns as collection ["427" , "Math"]
. How can I return only the lecture code which is the part that comes before the #
character.
You can use $arrayElemAt to return only first item from $split
result:
db.collection.aggregate([
{$project: {"lecture": {$arrayElemAt:[{$split: ["$lecture" , "#"]}, 0]}}}
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With