PyMongo – How to query the number of properties in the object

Question:

I need to retrieve numerical data from hundreds of documents looking like this one:

{
  "_id": "123",
  "Notes": {
    "1222": "Something is here"
  },
  "BehavioralData": {
    "Folder1": {
      "Sex": "Male",
      "Age": "22",
      "Date": "",
      "ResearchGroup": "",
      "Institution": "University of Manitoba"
    },
    "MoCA": {
      "Visual-Executive": "",
      "Naming": "NameHere",
      "Attention": "",
      "Language": "",
      "Abstraction": "",
      "Delayed Recall": "",
      "Orientation": "",
      "Education": "",
      "Total": ""
    }
  }
}

The output should look like this:

{
  "NotesLength": 1,
  "BehavioralLength": 2
}

Could you please suggest what tools I should use and how to get the result above? I am pretty sure that I should use aggregation for this problem, but I don’t know what commands would help.

UPDATE: I am writing a script in Python using the PyMongo library. Apparently, PyMongo has some problems with the $objectToArray part. I get the following error:

PlanExecutor error during aggregation :: caused by :: The argument to
$size must be an array, but was of type: null, full error: {‘ok’: 0.0,
‘errmsg’: ‘PlanExecutor error during aggregation :: caused by :: The
argument to $size must be an array, but was of type: null’, ‘code’:
17124, ‘codeName’: ‘Location17124’, ‘$clusterTime’: {‘clusterTime’:
Timestamp(1658726074, 53), ‘signature’: {‘hash’: b’L$xb3xcc
xdbxb0#f}xbaZxc6rBxf5xe6]x00xc3′, ‘keyId’:
7064465060042637317}}, ‘operationTime’: Timestamp(1658726074, 53)}

Answers:

With aggregation pipeline.

Since both Notes and BehavioralData are key-value pair, you need $objectToArray to convert the value to an array and next use $size to get the size of the array.

db.collection.aggregate([
  {
    $project: {
      _id: 0,
      "NotesLength": {
        $size: {
          $objectToArray: "$Notes"
        }
      },
      "BehavioralLength": {
        $size: {
          $objectToArray: "$BehavioralData"
        }
      }
    }
  }
])

Sample Mongo Playground

Answered By: Yong Shun