mongodb - ListField without duplicates in Python mongoengine -


I really should have some clarity but I can not find a way to represent a set using the mongoengine < Description = StringField (max_length = 50) parent = ListField (ReferenceField ('self')) = item.objects.get_or_create (Name = 'test item') [0] i2 = item (name = 'parent1') i2.save () i3 = item (name = 'parent3') i3.save () i.parents .append (i2) i. Parents.append (i2) i.parents.append (i3) i.save ()

The above code will create a duplicate entry for i2 in parent area of ​​i1. How do you express a foreign key like a relationship in Mongoengin? Instead of using save / <>

attachments instead of save Instead of using and to change MongoEngine to update, you can use the Nuclear Update and the $ addToSet method - see

then in your case you can:

  i.update (add_to_set__parents) = i2) i.update (add_to_set__parents = i3) i.update (add_to_set__parents = i2)   

addToSet And supports each currently present - see:

Comments