简体   繁体   English

使用find()在mongoDB中以字符串形式搜索唯一_id的列表

[英]Search list of unique _id as String in mongoDB with find()

I would like to find a list of different values with unique _id but the problem is that they are saved as String and if I try to search fe 10 it finds me also 100, 101, 102 etc.. In my case { "_id" : { "$gte" : "10" , "$lte" : "15"}} I would like to find 10, 11, 12, 13, 14, 15. But it finds me also 100, 110, 120 etc.. 我想用唯一的_id查找不同值的列表,但问题是它们另存为String,如果我尝试搜索fe 10,它也会发现我100、101、102等。在我的情况下, { "_id" : { "$gte" : "10" , "$lte" : "15"}}我想找到10、11、12、13、14、15。但是我也找到100、110、120等。 。

Thanks for your help 谢谢你的帮助

You have two more options: 您还有两个选择:

  • Issue a $regex query: {_id : /^1[0-5]/} (not sure about the PCRE syntax) 发出$regex查询: {_id : /^1[0-5]/} (不确定PCRE语法)
  • Add a new field whose value is the numeric equivalent of _id and query that new field 添加一个新字段,该字段的值等于_id的数值,并查询该新字段

You can do it using javascript expression 您可以使用javascript表达式来做到这一点

db.test.find("this._id > 10 && this._id < 15")

Please note that this will be a little slower 请注意,这会有点慢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM