I am struggling to create a list of documents whose ‘title’ field contains only one word.
Thank you
B
You can do it like this:
-
$split
- to split the title property by spaces, so you would get an array of all the words. -
$size
- to count the number of words. -
$eq
- to check if the number of words is equal to 1.
db.collection.aggregate([
{
"$match": {
"$expr": {
"$eq": [
{
"$size": {
"$split": [
"$title",
" "
]
}
},
1
]
}
}
}
])
2 Likes
Once again, thank you, this helps me understand the nested nature of the query.
BC
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.