0

I have following list in 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")
        },
}

Now I'm using mongoengine in my python app.

If I want to get comments's no: 2's seq field value(in this code, it is 25), how can I manipulate my query?

I found the docs related it(http://docs.mongoengine.org/guide/querying.html) But I don't know it is exactly same with what I want.

Any solution here?

Thanks!

Hide
  • 3,199
  • 7
  • 41
  • 83

1 Answers1

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

enter image description here

You can refer to it.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Krloy
  • 26
  • 1