Home > Software engineering >  How to list the outer-most fields of a large document usign PyMongo?
How to list the outer-most fields of a large document usign PyMongo?

Time:11-12

I am just having difficulties to understand this, how can I list outer-most fields when I am working with a huge text datasets? I am trying to implement it in Mongodb and pymongo? any suggestions?

CodePudding user response:

I am not sure what you need, but maybe the bellow can help.

Query

  • this query returns an array with all the names of the outer fields
  • objectToArray to convert ROOT document to array
  • get the first member that is the field name

Test code here

aggregate(
[{"$project": 
    {"_id": 0,
      "outer-fields": 
      {"$map": 
        {"input": 
          {"$map": 
            {"input": {"$objectToArray": "$$ROOT"},
              "in": ["$$m.k", "$$m.v"],
              "as": "m"}},
          "in": {"$arrayElemAt": ["$$this", 0]}}}}}])
  • Related