是否可以使用Mongo的“对象ID”作为其唯一标识符?如果是这样,我如何将它转换为字符串并查找它的字符串? [英] Is it ok to use Mongo's "Object ID" as its unique identifier? If so, how can I convert it to a string and look it up by string?
问题描述
例如...
user/view/?id=324gijsdfi3h25o1
我可以 str()
it ... but ...
I can str()
it...but...
我如何通过字符串查找
?
编辑:我想让Mongo中的每个文档有一个唯一的标识符(通常是字符串),我可以查找。我希望对象ID可以是这个(因为它有很多信件,它是唯一的。)我想要使用HTTP GET。 view?uid = e93jfsb0e3jflskdjf
推荐答案
您可以构造一个新的 ObjectId
使用字符串。此示例使用MongoDB控制台:
You can construct a new ObjectId
using the string. This example uses the MongoDB console:
db.users.find({ _id: ObjectId("4cdfb11e1f3c000000007822") })
我无法从您的问题中得知您使用的是哪种语言驱动程序驱动程序也支持此功能。
I can't tell from your question which language driver you are using (if any at all), but most drivers also support this functionality.
您不应将数据库中的 ObjectId
转换为字符串,然后比较它到另一个字符串。如果你这样做,MongoDB不能使用 _id
索引,它必须扫描所有文档,导致查询性能不佳。
You should NOT convert the ObjectId
in the database to a string, and then compare it to another string. If you'd do this, MongoDB cannot use the _id
index and it'll have to scan all the documents, resulting in poor query performance.
这篇关于是否可以使用Mongo的“对象ID”作为其唯一标识符?如果是这样,我如何将它转换为字符串并查找它的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!