I have got field in my Mongo table that is stored as a Hash.
"values" : {
"4e64b7cb3e9b3a4d8a00000b" : "Page",
"4e77304b3e9b3a4ace000003" : "Michael",
"4e64b7cb3e9b3a4d8a00000c" : 6,
"4e64b7cb3e9b3a4d8a00000d" : 4,
"4e64b7cb3e9b3a4d8a00000e" : 2,
"4e64b7cb3e9b3a4d8a00000f" : 6,
"4e64b7cb3e9b3a4d8a000010" : 12
}
Now I need to fetch all rows where values stores data equal to Michael
Like in Ruby:
Table.all.select{|row| row.values.any?{|k,v| v == 'Michael' }}
You can use multikeys to simulate a large number of indexes which makes such key/value queries more efficient.
valuesshould be an array, however, rather than a complex object with unknown schema, because that rules out indexing by definition.Because of the way MongoDB works with arrays, this will find all documents that have at least one element in
valueswithvalue“Michael”. Note, however, that this will find the parent document, not the key/value pair itself.