1

mongoengine で使用するaggregateと、mongoengine オブジェクト リストの代わりに CommandCursor が返されます。これは、mongonengine が実際には使用されないことを意味します。

例: ドキュメントにタイトル フィールドがない場合、エラーが発生します。結果を mongoengine オブジェクトに変換するにはどうすればよいですか?

class Post(Document):
    title = StringField(max_length=120, required=True)
    author = ReferenceField(User)

Host.objects()
# [<Post: Post object>, <Post: Post object>, ...]

pipeline = [
    {
        "$match": {
            'types': type,
        }
    },
    {
        "$project": {
            "name": 1,
            'brating': {
                "$divide": [
                    {"$add": ["$total_score", 60]},
                    {"$add": ["$total_votes", 20]}
                ]
            }
        }
    },
    {"$sort": {"brating": -1}},
    {"$limit": 100}

]

Host.objects.aggregate(*pipeline)
# <class 'pymongo.command_cursor.CommandCursor'>

list(Host.objects.aggregate(*pipeline))
# <class 'list'>
4

1 に答える 1