Hi,
Limit() is used with the find() command to retrieve the
specific count of data to the output. This is similar to TOP in Sql server. It will
restrict the count of data to the mentioned Quantity inside the limit function.
In the below code am retrieving the data from ‘store’
collection and am limiting to 2 records.
C#: .Net framework 4.5 and MongoDB 2.0
using System;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Core;
namespace LimitRecds
{
class Program
{
static void Main(string[] args)
{
CallMain(args).Wait();
Console.ReadLine();
}
static async Task CallMain(string[] args)
{
var conString = "mongodb://localhost:27017";
var Client = new MongoClient(conString);
var DB = Client.GetDatabase("test");
var collection = DB.GetCollection<BsonDocument>("store");
var list = await collection.Find(new BsonDocument())
.Limit(2) //retrive only two
documents
.ToListAsync();
foreach (var docs in list)
{
Console.WriteLine(docs);
}
}
}
}