1

I'm using azure cosmos DB acting as MongoDB version 4.0.0. I need to get N random documents from Azure Cosmos DB using the MongoDB API. I've tried using the $sample operator but it is giving me documents in the same order each time I query.

db.collectionName.aggregate([{$sample: {size: 1}}])

No matter how many times I run this query I'm getting the same document from the collection.

1 Answers1

0

I can't find any document on how $sample execute, but I think the problem we met may have relationship with what mentioned here. That means if some conditions are not met, it will get the result and sort them, then return the size count of items. So what we get always the same. I'll try to find more details, and any further find will update.

enter image description here

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29