The Find and FindAsync methods are used to to retrieve data from a collection in MongoDB and all queries in MongoDB have the scope of a single collection.
Follow the Install and Connect to MongoDB step to connect to a running MongoDB instance.
The below code will finds documents whose Name field equals “Himen”.
using System;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace MongoDB
{
class Program
{
static void Main(string[] args)
{
FindDoc(args).Wait();
Console.WriteLine("Press Enter");
Console.ReadLine();
}
static async Task FindDoc(string[] args)
{
var _connectionString = "mongodb://localhost:27017";
var _client = new MongoClient(_connectionString);
var _database = _client.GetDatabase("blog");
var _collection = _database.GetCollection("users");
var _list = await _collection.Find("{Name : 'Test user'}").ToListAsync();
foreach(var _doc in _list)
{
Console.WriteLine(_doc);
}
}
}
}
