简体   繁体   中英

Get a BsonDocument at a specific offset

I need to get a document from a collection at a specific point - is there any way of doing this, or would I need to append a value into the document structure? More specifically, I need to get the document based on a random number... Something like this...

Random rand = new Random();
int randNumber = rand.Next(1, (int)bsonCollection.Count());

var document = bsonCollection.findDocumentAt(randNumber);
//     the function I'm after ^ or equivilient

Edit: I think this is a better solution (I didn't know about ElementAt, as I've never actually needed to do this):

var document = bsonCollection.FindAll().ElementAtOrDefault(randNumber);

You should be able to achieve this using skip on the cursor. Something like:

var cursor = bsonCollection.FindAll();
cursor.Skip = randNumber;
var document = cursor.FirstOrDefault();

Or slightly neater:

var document = bsonCollection.FindAll().SetSkip(randNumber).FirstOrDefault();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM