How to set the hilo sequence starting value in Mon

2019-08-18 07:56发布

问题:

i imported a lot of existing values into my mongodb via the norm driver (including the "old" id - integer value). Now i got duplicate key errors from time to time.

To solve this, i have to set the starting value for the hilo sequence manually. How can this be done?

Thanks in advance

回答1:

The HiLo key information is stored in the NormHiLoKey collection. You can increment the value in this collection to change the starting value of the generated keys, using the following command in the Mongo shell:

db.NormHiLoKey.update({ _id: "nameOfCollection" }, { $inc: { ServerHi: 42 } })

CAUTION

Do not set the ServerHi value from the Mongo shell! The ServerHi is stored as a 64 bit integer, which cannot be represented in the shell. So if you set the value from the shell, it will change the underlying data type and break the NoRM deserializer.

If you run the db.NormHiLoKey.find() command, you'll likely see objects with floatApprox properties. This is an indication that the underlying data type is a 64 bit integer. By using the $inc operator you can safely modify the value, without accidentally breaking anything.



标签: mongodb norm