Is it possible to query for a range of hashes of a hashed indexed key in mongodb?

╄→гoц情女王★ 提交于 2019-12-12 02:48:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!