drizzt13
drizzt13

Reputation: 649

generate mongodb's ObjectId value from data in the doc

just to check if it is possible and if it worth the trouble:

i'm using python 3.9.5, with mongoengine as ORM.

lets say, i have data to to save in a collection:

{
    "value": string,
    "origin: string,
    "parent": string,
    ....
}

when the fields "value" and "origin" are unique together.

now, i want to be able to control the creation of the _id field, i.e, to generate the value of the ObjectId from those 2 values - that it will still be unique value, won't drop the performance, and yet - the value won't be some random value generated.

any thoughts about this? any ideas?

Update: what if i want to keep using objectId for the _id field?

for objectId, only 12/24 long strings are valid. so i took an idea from here, and wrote this piece of code (that create a hash 24 chars long):

import hashlib
from bson.objectid import ObjectId
the_string = "..............."
24_long_str = hex(int(hashlib.sha256(the_string.encode('utf-8')).hexdigest(), 16) % (10**28))[2:]
oid = ObjectId(24_long_str)

any thoughts about this code? performance issues? will the value be unique to be used as id?

Upvotes: 0

Views: 1070

Answers (1)

Buzz Moschetti
Buzz Moschetti

Reputation: 7568

If you assert value "plus" origin are truly unique, then just construct _id from both prior to insert. _id does not have to be autogenerated nor must it be an ObjectId type.

var d = {
    "value": "ABC",
    "origin": "here",
    "parent": "P"
};

// One possible format for combo of value + origin:
d['_id'] = d['value'] + "_" + d['origin'];
db.foo.insert(d);

Upvotes: 1

Related Questions