javascript - Mongodb find in hash by value - Stack Overflow

i have this mongodb documents format:{"_id": ObjectId("5406e4c49b324869198b456a"),&

i have this mongodb documents format:

    {
      "_id": ObjectId("5406e4c49b324869198b456a"),
      "phones": {
        "12035508684": 1,
        "13399874497": 0,
        "15148399728": 1,
        "18721839971": 1,
        "98311321109": -1,
      }
    }

phones field - its a hash of phone numbers and frequency of its using.

And i need to select all documents, which have at least one zero or less frequency.

Trying this:

    db.my_collection.find({"phones": { $lte: 0} })

but no luck.

Thanks in advance for your advices

i have this mongodb documents format:

    {
      "_id": ObjectId("5406e4c49b324869198b456a"),
      "phones": {
        "12035508684": 1,
        "13399874497": 0,
        "15148399728": 1,
        "18721839971": 1,
        "98311321109": -1,
      }
    }

phones field - its a hash of phone numbers and frequency of its using.

And i need to select all documents, which have at least one zero or less frequency.

Trying this:

    db.my_collection.find({"phones": { $lte: 0} })

but no luck.

Thanks in advance for your advices

Share Improve this question edited Jun 27, 2017 at 13:36 Neil Lunn 151k36 gold badges355 silver badges325 bronze badges asked Sep 10, 2014 at 9:42 remedgeremedge 131 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You can't do that sort of query in MongoDB, well not in a simple way anyhow, as what you are doing here is generally an "anti-pattern", where part of your data is actually being specified as "keys". So a better way to model this is you use something where that "data" is actually a value to a key, and not the other way around:

    {
      "_id": ObjectId("5406e4c49b324869198b456a"),
      "phones": [
        { "number": "12035508684", "value": 1 },
        { "number": "13399874497", "value": 0 },
        { "number": "15148399728", "value": 1 },
        { "number": "18721839971", "value": 1 },
        { "number": "98311321109", "value": -1 },
      }
    }

Then your query is quite simple:

db.collection.find({ "phones.value": { "$lte": 0 } })

But otherwise MongoDB cannot "natively" traverse the "keys" of an object/hash, and to do that you need do JavaScript evaluation to do this. Which is not a great idea for performance. Basically a $where query in short form:

db.collection.find(function() { 
    var phones = this.phones; 
    return Object.keys(phones).some(function(phone) { 
        return phones[phone] <= 0;
    }) 
})

So the better option is to change the way you are modelling this and take advantage of the native operators. Otherwise most queries require and "explicit" path to any "key" inside the object/hash.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745264619a4619379.html

相关推荐

  • javascript - Mongodb find in hash by value - Stack Overflow

    i have this mongodb documents format:{"_id": ObjectId("5406e4c49b324869198b456a"),&

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信