Tribe56123
Tribe56123

Reputation: 402

How to get 50 random documents from between 400 to 500 in a collation of 1000 documents?

I am using skip limit and aggregate but it's not working.

db.collation
.skip(400)
.limit(100)
.aggregate([{$sample: {size: 50}}])

What is the right way to do this?

Upvotes: 0

Views: 56

Answers (1)

dangarfield
dangarfield

Reputation: 2330

Try it as part of the aggregation pipeline:

Demo - https://mongoplayground.net/p/LhTNE85OZVS

db.collection.aggregate([
  {
    $skip: 400
  },
  {
    $limit: 100
  },
  {
    $sample: {
      size: 50
    }
  }
])

Upvotes: 2

Related Questions