0

I have the following Mongo structure

{
    "brand": "test",
    "keyword": "test",
    "user": "test",
    "link": "AAA",
    "text": "test",
    "insert_utc_ts": "2021-11-04 23:18:34.970",
    "update_utc_ts": "2021-11-04 23:45:55.561",
    "media": [{
        "src": "Test2",
        "media_ai_text": "Test",
        "media_type": "PHOTO_TEST",
        "insert_utc_ts": "2021-11-04 23:23:30.315",
        "update_utc_ts": "2021-11-04 23:23:30.315"
    }, {
        "src": "Test",
        "media_ai_text": "Test",
        "media_type": "PHOTO_TEST",
        "insert_utc_ts": "2021-11-04 23:23:45.331",
        "update_utc_ts": "2021-11-04 23:23:45.331"
    }]
}

I need to find the Media update_utc_ts where media.src = Test2 and link = AAA

I've tried to use this filter

{ link: "AAA", media.src: "Test2"}

But it's not working at all. Any help is appreciated Thx

2
  • 1
    Did you try { link: "AAA", "media.src": "Test2"}? Commented Nov 5, 2021 at 10:11
  • yes but not working Commented Nov 7, 2021 at 21:25

1 Answer 1

1

You can use $unwind to deconstruct the array and then $match by your desired value. And optional stage $project to return only the value you want:

db.collection.aggregate([
  {
    "$unwind": "$media"
  },
  {
    "$match": {
      "link": "AAA",
      "media.src": "Test2"
    }
  },
  {
    "$project": {
      "_id": 0,
      "media.update_utc_ts": 1
    }
  }
])

Example here

Note that I'm assuming you want to retrieve media.update_utc_ts and that's why your filter query doesn't work.

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

2 Comments

Thx, my problem is that I'm using a RobotFramework library, and I'm not able to use all mongo funtion/syntax. I was hoping to have a single "find" query including media.src and the link. it is possible?
Maybe $elemMatch into projection stage can helps you example but it only returns the first coincidence.

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.