-2

I have problem about get random selection of records in a table mongoDb

example in sql server:

select top 10 * from Employee order by NEWID()

now equivalent order by NEWID() in mongoDB C#

thank you

random selection of records in a table mongoDb


I mean was

Randomly selects the specified number of documents from the input documents

$sample

collection.AsQueryable<T>().Where().Select().**Sample(count)**.ToList()
ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 1
    I don't use MongoDB but, as far as I can see, `UUID()` would do basically the same job. For the record, I searched the web for "sql server newid" to find out that that function generates a GUID, then I searched for "mongodb generate guid" to find that `UUID` function. This is the sort of thing you should be doing for yourself before posting here. Questions that can answered in less than a minute on a search engine don't need to be asked here. – jmcilhinney Feb 16 '23 at 08:21
  • You did not understand my question @jmcilhinney – Saeed Safaee Feb 16 '23 at 09:31
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 16 '23 at 11:39

1 Answers1

0

If I understand what you need correctly, you should use aggregate $sample stage. There is no typed version of this stage in .net driver, see here how you can configure it via raw form.

dododo
  • 3,872
  • 1
  • 14
  • 37