I have a collection of documents containing an array of objects:
db.collection.insert({
arr: [
{ id: 1, text: 'foo' },
{ id: 2, text: 'bar' },
]
});
Is there a way to extract/project/add a field of one element in that array? For example, the text field of the first element of the array. I've tried various variations of $addFields in MongoPlayground,
db.collection.aggregate([
{
$addFields: { text1: '$arr.text' }
}
]);
but nothing produced just one text field. At best, I got both, with the syntax above, but I want only one field, in order to use $type on it, because it appears $type can't inspect array elements.