Let's say I have schema VideoPosts
. VideoPosts
has an array field comment
.
[
{
"user": "1",
"post": "1",
"comment": [
{
from: "Gene",
message: "Awesome!"
},
{
from: "Dash",
message: "Great job!",
imageAttachment: "www.image.com/imageurl"
}
]
},
{
"user": "1",
"post": "2",
"comment": [
{
from: "Bill",
message: "Good video tutorial!"
}
]
},
{
"user": "2",
"post": "3",
"comment": [
{
from: "Bill",
message: "This video helped me!"
}
]
},
{
"user": "3",
"post": "4",
"comment": [
{
from: "Bill",
message: "I ran into this error:",
imageAttachment: "www.image.com/imageurl"
}
]
}
]
I want to find the VideoPost
where there is at least one comment with an imageAttachment. In the above example that means post 1 and post 4. I want to use aggregate because I'm going to combine it with some other filters.
I tried this but it did not work
db.collection.aggregate([
{
$match: {
"comments.imageAttachment": {
"$exists": true
},
},
},
])
CodePudding user response:
it seems just a typo, have you tried same query with comment in singular form?
db.collection.aggregate([
{
$match: {
"comment.imageAttachment": {
"$exists": true
},
},
},
])