2
votes

I want to remove ReferenceField value in mongoengine

My Model:

class User(db.Document):
    name = StringField(required=True)
    last_name = StringField()
    address_ref = ReferenceField(Address)



class Adress(db.Document):
    street = StringField()
    city = StringField()
    state = StringField()

Unfortunately in mongengine documentation. There is not much information about unset. So I'm trying something like this

User.update(id=ObjectId("59ca38c5d1174d081c9bcaca")).unset(address_ref="").first()

but I have an error

TypeError: update() missing 1 required positional argument: 'self'

My Document

{
    "_id": {
        "$oid": "59ca38c5d1174d081c9bcaca"
    },
    "name": "Amanda",
    "last_name": "Barow",
    "address_ref": {
        "$oid": "59ca38c9d1174d081c9bcad7"
    }
},
{
    "_id": {
        "$oid": "59ca38c4d1174d081c9bcac9"
    },
    "name": "Larry",
    "last_name": "Denor",
    "address_ref": {
        "$oid": "59c8f34200fc624a0168c4bc"
    }
}

I want to get this effect (Amanda adress_ref = "" )

{
    "_id": {
        "$oid": "59ca38c5d1174d081c9bcaca"
    },
    "name": "Amanda",
    "last_name": "Barow",
    "address_ref": ""
},
{
    "_id": {
        "$oid": "59ca38c4d1174d081c9bcac9"
    },
    "name": "Larry",
    "last_name": "Denor",
    "address_ref": {
        "$oid": "59c8f34200fc624a0168c4bc"
    }
}
1

1 Answers

0
votes

Your update causes a TypeError because you are calling this method on the class level - you should be calling it on a document/instance. That's why the TypeError mentions the absence of the first positional argument (self) which is the instance to be updated.

So your update line should work like that:

User.objects().first().update(address_ref="")

If the update works, that line will return 1.