V
V
Vampre2021-05-08 17:40:29
MongoDB
Vampre, 2021-05-08 17:40:29

How to get the range of elements of a mongodb collection?

The database contains something like this

[
  {'job': 'Ecologist', 'company': 'Eaton-Romero'}
  {'job': 'Environmental education officer', 'company': 'Roman-Lee'}
  {'job': 'Water quality scientist', 'company': 'Gillespie Inc'}
  {'job': 'Technical author', 'company': 'Clark Group'}
  {'job': 'Physiological scientist', 'company': 'Smith, Nguyen and Matthews'}
  {'job': 'Colour technologist', 'company': 'Robinson PLC'}
  {'job': 'Radio producer', 'company': 'Coleman, Reyes and Austin'}
  {'job': 'Designer, graphic', 'company': 'Thomas Inc'}
  {'job': 'Engineer, broadcasting (operations)', 'company': 'Le Ltd'}
]

I need to get a slice from the collection, I found the $slice option, but I need to specify the array field there, and I need the entire array of collection objects
db.collection.find(
   <query>,
   { <arrayField>: { $slice: <number> } }
);

that is something like this:
db.collection.find({}, { <arrayField>: { $slice: [start, end] })

what should be specified instead of arrayField in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
V-ampre, 2021-05-11
@Vampre

I chose the wrong way - you need to use limitinstead skipof slice
https://docs.mongodb.com/drivers/node/fundamentals...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question