| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- # https://github.com/MongoEngine/mongoengine
- '''
- 真难用,完全不兼容 pydantic ,并且没有方便的 dict 转换,而且搜索语句不是人类友好的方式,是作者自己定义的操作语句,完全倒反天罡。
- '''
- from datetime import datetime
- from pydantic import BaseModel
- from typing import List, Dict, Optional,TypedDict
- from pymongo import MongoClient
- from mongoengine import *
- # test 数据库,没有则创建
- connect('test', host='sv-v2', port=27017)
- class Variant(EmbeddedDocument):
- name = StringField()
- price = FloatField()
- description = StringField()
- category = StringField()
- # 集合名称为 product
- class Product(Document):
- name = StringField()
- price = FloatField()
- description = StringField()
- image = StringField()
- brand = StringField()
- category = StringField()
- variant = ListField(EmbeddedDocumentField(Variant))
- def test_get_product():
- product = Product(
- name="数据线2",
- price=100,
- description="产品描述",
- image='123123',
- variant=[
- Variant(
- name="3M,蓝色",
- price=100,
- description="蓝色数据线",
- ),
- ]
- )
- return product
- def insert_object():
- product = test_get_product()
- product.save()
- def update_one():
- product:Product = Product.objects(name='数据线2').first()
- print(product.to_json())
- print(product.__weakref__)
- def main():
- # insert_object()
- update_one()
- if __name__ == "__main__":
- main()
|