$project不返回指定的字段

2024-09-27 19:25:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我的文档结构

enter image description here

这是我正在运行的。。。。你知道吗

query = self._database.suiteruns.aggregate([
    {
        "$project": {
            "name": 1,
            "user": 1,
            "endTime":1,
            "duration": 1,
            "testCases": 1,
            "verdict": 1
        }
    },
    {
        "$match": {
            "user": user_id
        }
    },
    {
        "$group": {
            "_id": "$name",
            "count": {
                "$sum": "$name"
            }
        }
    },
    {
        "$sort": {
             "updatedTime": -1
        }
    }
])

运行上述查询后的结果

[
  {
   u'count': 0, 
   u'_id': u"Minor GC003"
  }
  .
  .
  .
  . some more objects like this(without the fields mentioned in $project)
]

*预期结果*

[
{
 "verdict": "",
 "testCases" : [{}],
 .
 .
 . (other fields)
}]

我想对name字段进行分组,并按用户进行匹配,还希望得到其他字段,如我在$project中提到的,但仍然没有得到其他字段。 任何帮助/讨论都会很好。。。


Tags: name文档selfprojectidfieldscountquery
1条回答
网友
1楼 · 发布于 2024-09-27 19:25:56

你可以试着跟着方式:-你知道吗

self._database.suiteruns.aggregate(
   [
      {$match : {"user": user_id}},
      { $group : {
           "_id" : "$name", 
           "result": { $push: "$$ROOT" },     
           "count": 
           {
                "$sum": "$name"
           } 
        }
     }
   ]
)

结果将是

[
  {
    'count': 0, 
    '_id': u"Minor GC003",
    'result':[{
                'name': 'abcd',
                //All the fields of root level
             }]
  }
]

希望这会有帮助。有关详细信息,请参阅$group。你知道吗

相关问题 更多 >

    热门问题