I have a database with documents like this:
{
_id: 123,
metrics: [{
_id: ObjectID(…),
name: 'foo',
steps: [1, 2, 3, …],
values: [10, 12, 13, …]
},
{
_id: ObjectID(…),
name: 'bar',
steps: [1, 2, 3, …],
values: [1.2, 1.2, 1.3, …]
},
]
}
Now my objective is to retrieve all documents, but $map
all array entries to their name
attribute, discarding the other fields. I thought I could do it like this:
pipeline = [{'$project': {'metrics': {'$map': {'input': '$metrics', 'in': '$this.name'}]
results = client.db.runs.aggregate(pipeline)
But the result has just None
values for each array entry ({'metrics': [None, None, None, …]}
), as if the requested field name
did not exist. But it does. I've also tried with the id field, to no avail. I can see in inspecting the data that the fields are there. I can't figure out what I'm doing wrong here.