简体   繁体   中英

Attempting a FindAll with C# MongoDB driver

I'm attempting a "find all" query using the latest version of the C# MongoDB driver.

The code looks a bit like this

var xx = _context.NLMCDatabase.GetCollection<BsonDocument>("Requestables").Find<BsonDocument>(new BsonDocument());
var qq = xx.ToListAsync<BsonDocument>();
long yy = await xx.CountAsync();

The database connection is fine I use the same connection for inserting data. I have sent an empty query as

new BsonDocument()

The code executes but does not seem to return any results. When exploring with the debugger it reports

在此处输入图片说明

You are missing the await for the xx.ToListAsync<BsonDocument>(); which means the value of qq will just be a task.

Try either:

var xx = _context.NLMCDatabase.GetCollection<BsonDocument>("Requestables").Find<BsonDocument>(new BsonDocument());
var qq = await xx.ToListAsync<BsonDocument>();
long yy = await xx.CountAsync();

or

var xx = _context.NLMCDatabase.GetCollection<BsonDocument>("Requestables").Find<BsonDocument>(new BsonDocument());
var qq = xx.ToListAsync<BsonDocument>();
long yy = await xx.CountAsync();
var listOfResults = await qq;

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