Hey Dan,
Thanks for the help. Unfortunately I am still running into the same
problems. Namely, _id is set to a randomly generated id, not the id
passed to .objects() and there is no mongoengine boilerplate saved in
mongo (ex. no "_cls" field). Further, I dont think I can pass in _id
to the update command as a hack to get around this?
Is there a way to atomically upsert using mongoengine such that any
object that must be created is identical to an object created via:
obj = ClassName(_id="id3", field=1,field2=1)
obj.save()
For example, assume I had a simple counter class:
class Counter(mdb.Document):
_id = StringField(primary_key=True) #counter name
count = IntField(default=0)
and I want to be able to atomically increment/create it. i could try
the following:
counter_name=...
Counter.objects(_id=counter_name).update(upsert=True, inc__count=1)
works great if the _id is alread there. However, this will write a doc
with a randomly generated id if not
Further, I could try:
obj, created = Counter.objects.get_or_create(_id=counter_name,
defaults={'count': 1})
if not created:
update....
However, that seems inefficient as i have to hit the db twice?
Further, I could modify the class to use a mongo generated id, but
then each doc would have to store 3 fields instead of 2
Let me know if I'm missing something or thinking about this the wrong
way. Any help is greatly appreciated!