正常插入
1 | Places(name="test", loc=[-87, 101]).save() |
批量
1 | Places.objects.insert([Places(name="test", loc=[-87, 101]), Places(name="test 2", loc=[-87, 101])]) |
效率
个人测试得出批量插入的速度没有将结果整理为一个字典,然后进行循环save保存
插入字典
mongoengine可以直接插入一个字典1
2a={"name0":"zhangsan","age":"11"}
User(**a).save()
常见错误
字段不能叫做ID,因为默认有一个,所以保存的时候会报下面错误1
2
3
4
5
6
7
8
9
10class User(Document):
id = IntField(required=True, default=-1)
name = StringField(max_length=200, required=True)
phone = StringField(max_length=200, required=True)
email = EmailField(required=True)
gender = StringField(max_length=200, required=True)
age = IntField(required=True, min_value=10, max_value=100)
idcard = StringField(max_length=200, required=True)
point = IntField(default=0)
is_admin = BooleanField(default=False)
错误1
2 raise ValidationError(message, errors=errors)
mongoengine.errors.ValidationError: ValidationError (User:None) (Invalid Object ID: ['auto_id_0'] Field is required: ['id'])