1

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
      },
      
    },
    
  },
  
])

playground

1 Answer 1

3

it seems just a typo, have you tried same query with comment in singular form?

db.collection.aggregate([
  {
    $match: {
      "comment.imageAttachment": {
        "$exists": true
      },
      
    },
    
  },
  
])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.