Mongoengine - 嵌入式文档过滤

时间:2013-11-04 13:45:13

标签: python django mongodb mongoengine

我正在使用Project处理Participants as embedded documents模型。以下是存储它的结构。

{ "_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" }

我想检索一个带有已接受参与请求的项目列表(即participant.status =“已接受”)

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

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

1 个答案:

答案 0 :(得分:9)

尝试在mongoengine中使用别名为match的{​​{3}}:

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