mongoengine 在嵌套字段上获取值

mongoengine get value on nested field

我在 mongodb 中有以下列表。

> db.article.find().pretty()
{
    "_id" : ObjectId("5bebcfbb1b48d9974aac78ee"),
    "no" : 40,
    "subject" : "string",
    "content" : "string",
    "userid" : "string",
    "comments" : [
        {
            "no" : 1,
            "content" : "First content",
            "userid" : "john",
            "parent" : null,
            "seq" : 12,
            "created_at" : ISODate("2018-11-14T16:33:01.943Z")
        },
        {
            "no" : 2,
            "content" : "Second",
            "userid" : "doe",
            "parent" : null,
            "seq" : 25,
            "created_at" : ISODate("2018-11-14T16:33:01.943Z")
        },
}

现在我在 python 应用程序中使用 mongoengine

如果我想得到comments的no:2的seq字段值(在这段代码中,它是25),我该如何操作我的查询?

我找到了相关的文档(http://docs.mongoengine.org/guide/querying.html)但是我不知道它和我想要的完全一样。

这里有什么解决办法吗?

谢谢!

db.getCollection('article').aggregate([
{"$project":{"comments.seq":1,"comments.no":1,"_id":0}},
{"$unwind":"$comments"},
{"$match":{"comments.no":NumberInt(2)}},
{"$project":{"comments.seq":1}}
])

你可以参考一下