0

I've inserted this document into db.references collections:

{
   "idRef":"asdf-ggtt-001",
   "metadades":[
      {
         "departament":"JUST",
         "changed": ISODate("2016-02-10T10:50:42.389Z")
      },
      {
         "ambit":"AMB1"
      }
   ]
}

So, I'd like to get which documents have a metadades.changed informed, where metadades is an array of nested documents.

Any ideas?

3
  • Apologies but I didnt quite understand your second last sentence. Are you looking for documents in which the metadates.changed field exists in atleast one of the nested documents? Commented Sep 20, 2018 at 10:52
  • Yes, it could be valid. Commented Sep 20, 2018 at 10:53
  • what should be the expected output? Commented Sep 20, 2018 at 11:31

1 Answer 1

1

For the full document (parent with nested documents):

db.references.find({"metadades.changed": {$exists: 1}}).pretty()

This will basically produce all documents with at least one nested document with the changed field present.

If you only want the nested document that matched:

db.references.aggregate([
{
    $unwind: {
        path: "$metadades"
    }
},
{
    $match: {
        "metadades.changed": {$exists: 1}
    }
},
{
    $replaceRoot: {
        "newRoot": "$metadades"
    }
}
])
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.