gpt4 book ai didi

python - 从 mongoengine 中的嵌入文档中删除值

转载 作者:行者123 更新时间:2023-11-30 23:40:31 25 4
gpt4 key购买 nike

我有一个名为 login_info 的文档其中包含以下字段:

class login_info(Document):
user_name = StringField(max_length=120)
password = StringField(max_length=120)
email = EmailField()
gender = StringField(max_length=120)
date_of_birth = DateTimeField()
visibility = StringField(max_length=120)
client_id = ObjectIdField(required=False)
location = ListField(EmbeddedDocumentField("Tracking"))`

在上述字段中location是一个嵌入文档,包含以下字段:

  • 时间

  • 纬度,

  • 经度

我想从此嵌入文档中删除满足以下条件的值。

time < system_datetime .

以下是 login_info 中的示例数据文档:

{
"_cls":"login_info",
"_id":ObjectId("5046f43c12d0592e3f59e25d"),
"_types":[
"login_info"
],
"date_of_birth":ISODate("2011-02-07T00:00:00 Z"),
"email":"jack@gmail.com",
"expire":1346827684,
"gender":"male",
"issue":1346827324,
"key":"47d1e64e51dfa1cf99ce4a59e0c940",
"location":[
{
"Latitude":"5615.3111",
"_types":[
"Tracking"
],
"_cls":"Tracking",
"Longitude":"1236.711",
"time":ISODate("2012-09-13T12:24:36.051 Z")
},
{
"Latitude":"000",
"_types":[
"Tracking"
],
"_cls":"Tracking",
"Longitude":"3.70",
"time":ISODate("2012-09-25T18:30:57.756 Z")
},
{
"Latitude":"11",
"_types":[
"Tracking"
],
"_cls":"Tracking",
"Longitude":"1",
"time":ISODate("2012-09-26T10:25:29.157 Z")
},
{
"Latitude":"11",
"_types":[
"Tracking"
],
"_cls":"Tracking",
"Longitude":"1",
"time":ISODate("2012-09-26T10:40:58.895 Z")
},
{
"Latitude":"11",
"_types":[
"Tracking"
],
"_cls":"Tracking",
"Longitude":"1",
"time":ISODate("2012-09-26T10:54:08.361 Z")
},
{
"Latitude":"11",
"_types":[
"Tracking"
],
"_cls":"Tracking",
"Longitude":"1",
"time":ISODate("2012-09-26T11:08:55.873 Z")
}
],
"password":"jack",
"refresh_token":"22580a8f69",
"token":"bac8a5f863",
"user_name":"jack",
"visibility":"visible"
}

最佳答案

如果您想使用 MongoEngine 从 login_info 文档中删除单个 location 嵌入文档,请执行以下操作:

// add 
loc1 = Tracking( time=datetime(2011, 11, 5, 0, 0, 0) )
loc2 = Tracking( time=datetime(2012, 10, 5, 0, 0, 0) )

login = login_info( user_name='Mark', location=[loc1, loc2] )
login.save()

// remove locations from the location list
login.objects( title=user_name='Mark' ).update_one( pull__lt=datetime.now() )

注意:mongoengine 0.7.5 有一个 bug ,因此您必须从 https://github.com/MongoEngine/mongoengine 下载最新版本

关于python - 从 mongoengine 中的嵌入文档中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12598832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com