2

I have a collection with documents like:

{   
   map:{
     key1:value1,
     key2:value2, 
     ....
    }
}

and I want to filter documents based on map values only, regardless the keys, something like map.* equal some value. Is it possible to do that?

MongoDB version used is 3.4

1
  • 1
    You can use $arrayToObject and objectToArray aggregation. Something like this db.collection.aggregate([ { $project: { map: { $objectToArray: "$map" } } }, { $match: { "map.v": "value1" } }, { $project: { map: { $arrayToObject: "$map" } } } ]) Commented Sep 24, 2018 at 8:57

1 Answer 1

6

You can try below aggregation using $objectToArray and $arrayToObject aggregation operators

db.collection.aggregate([
  { "$project": { "map": { "$objectToArray": "$map" }}},
  { "$match": { "map.v": "value1" }},
  { "$project": { "map": { "$arrayToObject": "$map" }}}
])
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.