问题
I have a hashed based sharding enabled for a key "userId" in a mongodb collection. Is it possible to get all documents whose hashed values of "userId" are between, let's say, "abcd" and "cdef"?
Thanks!
回答1:
One way that I found is to use the following query:
db.mycollection.find({
"$and": [
{_id: {"$gt": -5012941854059245285}},
{_id : {"$lt": -4712634714892678072}}
]}).hint({_id: "hashed"})
where the long numbers are hashed values of _id. It works for me, but is it the best way of doing so? Can I be sure that it will work with the upcoming releases of mongodb?
来源:https://stackoverflow.com/questions/17726236/is-it-possible-to-query-for-a-range-of-hashes-of-a-hashed-indexed-key-in-mongodb