gpt4 book ai didi

python - Mongoengine - 嵌入式文档过滤

转载 作者:IT老高 更新时间:2023-10-28 13:33:21 24 4
gpt4 key购买 nike

我正在研究 Project 模型,其中 参与者作为嵌入文档。以下是存储它的结构。

{ "_id" : ObjectId( "5277a15c2d6d1302a2a9bf88" ),
"code_certified" : true,
"description" : "This is gonna to be accepted.",
"owners" : [
"5277a1472d6d1302a2a9bf86" ],
"participants" : [
{ "id" : ObjectId( "5277a15c2d6d1302a2a9bf87" ),
"invitee" : { "email" : "pravin@gmail.com",
"name" : "P",
"id" : "5277a1472d6d1302a2a9bf86" },
"inviter" : { "email" : "pravin@gmail.com",
"name" : "P",
"id" : "5277a1472d6d1302a2a9bf86" },
"role" : "owner",
"date_invited" : Date( 1383523200000 ),
"status" : "accepted" },
{ "id" : ObjectId( "5277a17f2d6d1302a2a9bf8d" ),
"invitee" : { "id" : "5277a1282d6d1302a2a9bf85",
"name" : "Pravin Mhatre",
"email" : "pravinhmhatre@gmail.com" },
"inviter" : { "id" : "5277a1472d6d1302a2a9bf86",
"email" : "pravin@gmail.com",
"name" : "P M" },
"role" : "contributor",
"date_invited" : Date( 1383523200000 ),
"status" : "pending" } ],
"task_sequence" : 1,
"title" : "Accept" }

我想检索已接受参与请求的项目列表(即参与者.status = "accepted")

我正在尝试使用以下代码。但它会返回所有项目。

ApiResponse(Project.objects.filter(participants__invitee__id=str(request.user.id), participants__status__iexact="accepted").all(), ProjectEncoder).respond()

最佳答案

尝试使用 $elemMatch别名为 match 在 mongoengine:

Project.objects.filter(participants__match={"status":"accepted",
"invitee.id":request.user})

关于python - Mongoengine - 嵌入式文档过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19769020/

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