I had an aggregate call that was working when revisions was an Object (with the keys being the entryId).
await Project.aggregate([
{ $limit: 1 },
{ $match: { _id: ObjectId(projectId) } },
{
$project: {
limits: 1,
revisions: { $slice: [`$revisions.${entryId}.${languageId}`, startIndex, PageSize] },
totalRevisions: {
$size: { $ifNull: [`$revisions.${entryId}.${languageId}`, []] },
},
},
},
]);
Now that I converted revisions to an array with entryId inside it, I'm not sure how to get the same result. I tried:
await Project.aggregate([
{ $limit: 1 },
{
$match: {
_id: ObjectId(projectId),
'revisions.entryId': ObjectId(entryId),
},
},
{
$project: {
limits: 1,
revisions: { $slice: [`$revisions.$.${languageId}`, startIndex, PageSize] },
totalRevisions: {
$size: { $ifNull: [`$revisions.$.${languageId}`, []] },
},
},
},
]);
But I get the error:
MongoError: FieldPath field names may not start with '$'.
How can I use $slice and $ifNull with an item in an array? Thanks
Sample doc:
{
"revisions" : [
{
"entryId" : ObjectId("5bbf8813c272e05171463bc4"),
"5bbe76c6d3fb1a4f143f8304" : [
{
"authorId" : ObjectId("5b1c5384d75d9f3b0eb65c2a"),
"revisionId" : ObjectId("5bbf8813c272e05171463bc7"),
"updated" : "2018-10-11T17:27:47.842Z",
"value" : "County"
}
]
}
]
}
$matchhas been used if applied$limitfirst!!! And please post the sample document and output as well