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

Time:11-30

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?

CodePudding user response:

Try it as part of the aggregation pipeline:

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

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