0

I want to filter my MongoDB collection by a cutoff value. My db is designed as such:

{
"_id" : "178666a8393c643d3f29258616408879",
"sims" : {
    "192ba2f580f69b53187a6c40c9225d0b" : 0.9912074804306030,
    "d8a35b15f23bcd2d57e38406ace0fe3e" : 0.9840455055236816,
    "d0f5018c07a694c35037cfae1f47021a" : 0.9687452912330627,
    "61440ff39598ea8c14678da6f09e6c43" : 0.9722751975059509,
    "d3e643b5b5e6abf2369da1aec12756cc" : 0.9448012113571167,
    "c12e7f447958a496d989640a56575eeb" : 0.8973514437675476,
    "5157fd7e3486a1c0fabccbee307e4e02" : 0.9997683167457581,
    "ea5d4a218500f503daf7d69e39b7ad0f" : 0.9960342049598694,

},

"_id" : "ea5d4a218500f503daf7d69e39b7ad0f",
"sims" : {
    "192ba2f580f69b53187a6c40c9225d0b" : 0.9979836344718933,
    "d8a35b15f23bcd2d57e38406ace0fe3e" : 0.9958176016807556,
    "d0f5018c07a694c35037cfae1f47021a" : 0.9867590069770813,
    "61440ff39598ea8c14678da6f09e6c43" : 0.9884092211723328,
    "d3e643b5b5e6abf2369da1aec12756cc" : 0.9697993993759155,
    "c12e7f447958a496d989640a56575eeb" : 0.9323428869247437,
    "5157fd7e3486a1c0fabccbee307e4e02" : 0.9973729848861694,
    "ea5d4a218500f503daf7d69e39b7ad0f" : 1.0000001192092896,
}

}

Now, I want to have a new db with all items with "sims" values e.g. > 0.99.

I tried

db.getCollection('similarity_test').find({ "sims.value": { $gte: 0.99 } } )

and similar attempts with db.aggregate but I fail to filter through all "_id"s and all "sims". I am restricted to using MongoDB 3.0, so I can't apply the $filter functionality.

Edit:

After removing named keys:

{
"_id" : "178666a8393c643d3f29258616408879",
"sims" : [ 
    {
        "sim" : 1.0000001192092896,
        "key" : "178666a8393c643d3f29258616408879"
    }, 
    {
        "sim" : 0.9960342049598694,
        "key" : "ea5d4a218500f503daf7d69e39b7ad0f"
    }], 

"_id" : "ea5d4a218500f503daf7d69e39b7ad0f",
"sims" : [ 
    {
        "sim" : 0.9960342049598694,
        "key" : "178666a8393c643d3f29258616408879"
    }, 
    {
        "sim" : 1.0000001192092896,
        "key" : "ea5d4a218500f503daf7d69e39b7ad0f"
    }, 
    {
        "sim" : 0.9971756935119629,
        "key" : "4160ee49e3e08ee237080c5b20dcfec7"
    }, 
    {
        "sim" : 0.9877796173095703,
        "key" : "f19ae8636304f8804f59c534a1fbc1d7"
    }],
}
oeb
  • 189
  • 1
  • 12
  • 1
    You cannot do that in a query. You have named keys and you should not be using named keys. – Neil Lunn Jun 20 '17 at 10:11
  • 1
    Use "arrays" instead: `"sims": [{ "key": "192ba2f580f69b53187a6c40c9225d0b", "value": 0.9912074804306030 },{ "key": "d8a35b15f23bcd2d57e38406ace0fe3e", "value": 0.9840455055236816 }]`. Then it's simple to query. – Neil Lunn Jun 20 '17 at 10:13
  • [Edit your question](https://stackoverflow.com/posts/44650095/edit) rather than posting in comments. – Neil Lunn Jun 20 '17 at 10:54

0 Answers0