Skip to main content

Projection in MongoDB with C# and .Net

    Projection is used to retrieve limited columns of data from the Mongodb collection. Here we can Include or Exclude the columns.

It is also used for retrieving the selected columns from the MongoDB collection, while using ‘include’ automatically _id column will be including we should manually ‘exclude’ _id column with ‘exclude’

using System;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core;

namespace Projections
  class Program
    static void Main(string[] args)
    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()).Project(Builders<BsonDocument>.Projection.Include("Price").Exclude("_id")).ToListAsync();
      foreach (var doc in list)


Post a Comment