hmarr/mongoengine — Issue #397: upsert object creation - creates wrong object
Author: grubberr
Created: 2011-12-22 15:58:29+00:00
State: open

from mongoengine import Document, connect, fields
connect('test')

class ChannelTermStats(Document):
time = fields.StringField(required=True, db_field="n")
term = fields.StringField(db_field='pe', unique_with='time')
count = fields.IntField(default=0, db_field='tc')

ChannelTermStats.objects.get_or_create(time='2011.12.22', term='like', count=1)

> db.channel_term_stats.findOne()
{
"_id" : ObjectId("4ef35195936aa23881000000"),
"_types" : [
"ChannelTermStats"
],
"n" : "2011.12.22",
"_cls" : "ChannelTermStats",
"pe" : "like",
"tc" : 1
}

but if we create new object with help of upsert

ChannelTermStats.objects(
time='2011.12.23', term='like').update_one(
safe_update=False, upsert=True, inc__count=1)

> db.channel_term_stats.findOne()
{
"_id" : ObjectId("4ef351dea6884e039ad326c5"),
"_types" : "ChannelTermStats",
"n" : "2011.12.23",
"pe" : "like",
"tc" : 1
}

you can see that "_types" field differ