-1

Here is the scenario. I am storing entries in MongoDB and using the _id as a public identifier for each entry when fetching them to display. Since the _id is fairly hard to guess and therefore a user with access to one entry based on the _id in the url can not guess the next or previous entry (except for the example below) based on their information. I also do not have to create extra indexes on another field containing an md5 hash or uniqid for example.

The problem: When creating multiple entries as part of an import job, entries get very similar _id´s due to the fact that they are partly based on timestamp. Three entries created during an import can look like this as an example. 52014b1a3c9fb7733d000000 52014b1a3c9fb7733d000001 52014b1a3c9fb7733d000002

In this scenario, a user might see the pattern and try to edit the last part of each identifier, and succeed in viewing another entry in this case.

My question(s): Is it possible to control the MongoId generation upon document creation so that it does not use only the increment function at the end when creating large amount if entries as an import for example? Changing the device part of the id? Can that be done?

My last resort is to stop using the _id as the public identifier for the entries, forcing me to set up additional indexes on a very large collection. I hope I do not have to go there.

I am using MongoDb in a PHP based application.

4

1 回答 1

1

您无法更改自动 ID 生成,因为它是所有驾驶员都必须使用的一套算法。但是,您可以自己创建自己的想法,但您必须为每个文档执行此操作:

<?php
$document = array(
    "_id" => new MongoID("ffb234234....");
);

您只需要确保在MongoID设置格式时将 24 个十六进制字符传递给构造函数。但是,如果您这样做,请确保您确实包含 PID 和哈希(主机名),以便您可以保证多个机器和进程之间的唯一性。

于 2013-08-08T10:02:11.817 回答