skip to Main Content

For example, I have

[
  { name: "John" },
  { name: "Mike" },
  { name: "Homer" },
  { name: "Bart" },
  { name: "Dmitry" },
  { name: "Dan" }
]

How will I choose many objects with mongoose, if I use .limit(2) I will get [{ name: "John" }, { name: "Mike" }], I need to choose [{ name: "Bart" }, { name: "Dmitry" }]. In default JS this method looks like .slice(3,5). How will I do it with mongoose?

2

Answers


  1. You can achieve this in mongodb by using skip(3).limit(2).

    In Mongoose you can achieve this with myModel.find({}, 'name', {skip: 3, limit: 2}), you just have to insert values of skip and limit you want in third parameter of find.

    Here’s documentation with an example of skip and here’s a link to a more popular answer of similar problem.

    Edit: Note that this is a short-sighted solution, you should use something else for large or changing database.

    Login or Signup to reply.
  2. You can try something like this:

    Model.find({name: {$in: ["Bart", "Dmitry"]}});

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search