MongoDB Tip & Tricks

Commands

Update a specific field value for a collection:

Example: Update “CampaignId” field across entire collection

db.Prospects.update(
{}, {$set: {CampaignId: ObjectId('54ec891dc8efe23e3e0fb1ef')}}, { multi: true }
)

Remove a specific field value for a collection:

Example: Remove “CampaignId” field across entire collection

db.Prospects.update(
   { CampaignId: "54ec891dc8efe23e3e0fb1ef" },
   { $unset: { ZipCode: ""} }
)

Filter out documents from a collection based on list of Id’s, equivalent to SQL “IN”
Example: Retrieve documents with list of “ProductId”s across entire collection.

List ids=new List();
for(int index=0;index<Products.Count;index++)
{
  ids.Add(ObjectId.Parse(Products[index].Id));
}
var filter = Builders.Filter.Eq("ProductId", ids);
return await _database.GetCollection("AnalyticsSummary")
.Find(filter)
.ToListAsync().ConfigureAwait(false);

Rename a specific field value for a collection:
Example: Rename “CampaignId” field to “CampaignName” across entire collection

db.getCollection('CampaignSummary').update({},{ $rename: { "CampaignId": "CampaignName" } }, false, true )

The false, true in the method above are: { upsert:false, multi:true }. You need the multi:true to update all your records.

MongoDB Bulk Data Import From CSV

Background

mongoimport is a very handy tool for bulk data import. For the most simplest form of importing data in your collection is is the only built in tool available. Alternatively for import one can built an utility .At present this tool as limited capability and can import data from Extended JSON, CSV, or TSV only.

For your help, I have provided an fully functional data import file with 1.3 million+ records. You can download it from here and use as you like.

Using Import Utility

The process of data importing is very easy. Let me elaborate the process by steps.

Create a *.csv file for import where column/header names are at first row.

1) Place the import data file under MONGO_ROOT/BIN/

2) From shell navigate to bin under MONGO installation directory

3) You have to execute following command

mongoimport -d [DB_NAME] -c tmpprospect --type csv --file 
[IMPORT_FILE_NAME].csv --headerline

If you take the import file I provided, command looks like this,
where DB_NAME is “hawk” & [IMPORT_FILE_NAME] is “hawk-prospect-data”

mongoimport -d hawk -c tmpprospect --type csv 
--file hawk-prospect-data.csv --headerline

4) You will see mongoimport utility will split the import file into small chunks of 10k records and importing one by one.

mongoimport

As you can see in the above screenshot, the csv file column name became property name in BASON document. So each record of CSV file will insert as a new BASON document is specified collection.

Limitations

1) In few occasions error messages while importing is not detailed enough to diagnosis.
2) This tool do not support importing to collection that consists of embedded documents, if you do so it will simply ignore the embedded document part and import rest of the documents.
3) Can only import into a single collection, at a time.

Whatever the limitations are, for the very basic importing need, this tool is time saver.

Solve Mongo DB Element does not match any field or property of class

Introduction

Due to non schema nature of mongodb collection, one collection may contains distinct number of fields.Its not recommended though, its possible to contain different number of fields due to business requirements. So for example say we have a collection “Client”, under which there is 2 document. Second document lacks 1 field “City”. So while binding back to POCO via c# driver , we get exception “Element does not match any field or property of class”.

sample-1

Solution

When a BSON document is deserialized back to POCO, the name of each element is used to look up a matching field or property in the class map, when deserializer do not find  the mapping property its throws an exception, that we have in this scenario. If we want to ignore those extra properties, there is two possible ways, during deserialization or during initialization of the BsonClassMap. I prefer the while I do the mapping, I live to keep my POCO clean.

sample-2

As you can see the above  code block,  SetIgnoreExtraElements methods is used to specify inside mapping to ignore extra elements from BSON document.

map.SetIgnoreExtraElements(true);

That’s it, now you can work with your POCO more easily.