Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MongoDB Slice set of documents

I want to get the nth position of a given collection.
However, all the slice methods I find, all refer to a given array in the document schema. In this case, I want to get the nth document of the collection. I tried $, but it gives me a syntax error:

result = await db.collection(col_name).find(
    { projection: { $: { $slice: [0, nPosition] } } }
).sort({ timestamp: -1 }).toArray();

Any clue how to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

use limit and skip :

result = await db.collection(col_name).find().sort({ timestamp: -1 }).skip(nPosition).limit(1).toArray();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading