4

Recently I started using MongoDB.I have to read specific fields(columns) from mongodb using mongodb C# driver.That means i have to read specific fields no matter whatever the values.i need to just specify the fields.I have unstructured data in my db.so i have no model class in my project.

I read Collection using Getcollection<> from the C# library.then after i stucked with this task.

How can i achieve?

Ajas Aju
  • 725
  • 7
  • 30

1 Answers1

8

There are a few ways you can achieve this depending on if your unstructured data is known at compile time or runtime.

For compile type, you can model your projection of the data and use the projection builder to specify how your projection should work

var collection = database.GetCollection<Customer>("customers");

var document = new Customer(){Name = "Joe Bloggs", Age = 30, Address = "York"};
collection.InsertOne(document);

var projection = Builders<Customer>
                    .Projection
                    .Include(x => x.Id).Include(x => x.Age);

var customerProjection = await collection.Find(x => true)
                    .Project<CustomerProjection>(projection)
                    .FirstAsync();

Above we specified the return type as the generic argument but if we omit this then we'll be returned a BsonDocument which can be useful depending on your usage

var bsonDocument = await collection.Find(x => true)
                    .Project(projection)
                    .FirstAsync();

We can also achieve the same result by using linq expression:

var projection = await collection.Find(x => true)
    .Project(x => new {x.Id, x.Age}).FirstAsync();

This will result in returning an annomous type with an Id and Age.

However if we don't know the data at compile time and are basing the fields of magic strings at runtime then you'll need to pass in BsonDocument to the GetCollection method:

var collection = database.GetCollection<BsonDocument>("customers");

You'll now be able to do both of the above methods to project the bson document but it will be on a per field basis.

However, I'd advise trying to use the Project builders as it will make your life a little easier:

var projectionDefinition = Builders<BsonDocument>.Projection
                                        .Include("age")
                                        .Exclude("_id");

var projection = await collection.Find(x => true)
                    .Project(projectionDefinition)
                    .FirstAsync();
Kevin Smith
  • 13,746
  • 4
  • 52
  • 77
  • It is a nice explanation,But my issue is i have no proper model ,why because my mongodb documents contains different fields for different documents.so i cant predict the exact field.so i am reading collection as Bson Document.Like var collection = _dbContext._database.GetCollection("CarModel"); – Ajas Aju Oct 30 '17 at 09:31
  • You must know something about the data to query and project it though? Even if it's the user passing in the data – Kevin Smith Oct 30 '17 at 09:39
  • I need to return either the non-null field value or the string "Unspecified" if the field is null or does not exist in the Projection(Include).now its not showing the field name if it does not exist the field – Ajas Aju Oct 30 '17 at 10:37
  • Do you not know the field names then? – Kevin Smith Oct 30 '17 at 12:01
  • yeh ,i read all the distinct field names from the collection.so i have field names....so how can i return either the non-null field value or the string "Unspecified" if the field is null or does not exist in the Projection(Include).please show me with one example field name.its too urgent...Thanks in advance. – Ajas Aju Oct 30 '17 at 12:44
  • 1
    You'll need to do a query with the Exists("...") operator - https://docs.mongodb.com/manual/reference/operator/query/exists/ – Kevin Smith Oct 30 '17 at 12:57