Initial "chat" document. Messages stored in "messages" array:
{"_id" : ObjectId("65d64029a16e5d5a9d00f1e6"),"name" : "Untitled chat","messages" : [ {"_id" : ObjectId("65dfbba726752480ef0c4167"),"text" : "message 1" }, {"_id" : ObjectId("65dfbba726752480ef0c4167"),"text" : "message 2" } ]}
Im trying to get last N messages from this array.I also want to apply limit and skip options.
Initially i used separate collection for messages. And my query was:
$cursor = $DB->messages->find( ['_id' => new MongoDB\BSON\ObjectID($cid)], ['sort' => ['_id' => -1], 'limit' => $limit, 'skip' => $skip] );
After several attempts using different variations of aggregate() and find(), I still haven't found a solution.
Language - PHP. MongoDB driver + MongoDB Atlas.