Programing QA

Programing/Developing Questions & Answers. Focus on business application material

Connect to MongoDB & execute string query using MongoDB DOT.NET Provider & Linq

Step 1: Download the MongoDB DOT.NET Provider from

Step 2: reference two DLLs in your project: “MongoDB.Driver.dll” & “MongoDB.Bson.dll”.

Step 3: Add “Using” statement:

Code Snippet
  1. using MongoDB.Bson;
  2. using MongoDB.Driver;


Step 4: Connect to MongoDB using C#:

Code Snippet
  1. string CONNECTION_STRING = "mongodb://myserver:port";
  3. MongoClient client = new MongoClient(CONNECTION_STRING);
  4. MongoServer server = client.GetServer();
  6. IEnumerable<string> databases = server.GetDatabaseNames(); // here we can get list of all databases on server
  8. MongoDatabase db = server.GetDatabase("MyDataBase"); // but we only need one.
  10. IEnumerable<string> collections = db.GetCollectionNames(); // get the collection we want to execute the query on.
  12. var collection = db.GetCollection<BsonDocument>("MyCollection");
  14. var jsonQuery = "{start:{ $gte: 1395828292287}}";//sample json query as text;
  16. BsonDocument doc = MongoDB.Bson.Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonQuery); // creating bson doc out of the query text.
  18. QueryDocument query = new QueryDocument(doc); // create query doc out of the bson doc.
  20. var toReturn = collection.Find(query); // execute the query - it's linq, nothing is sent to the server yet.
  22. List<BsonDocument> result = toReturn.Take(100).ToList(); // now it's here.

Comments (1) -

  • Oswaldo Lairsey

    5/18/2014 7:30:42 AM |

    Hi there, after reading this amazing article i am also delighted to share my experience here with friends.|

  • eebest8 john

    3/11/2015 1:12:33 AM |

    Really enjoyed this article.Thanks Again. Want more.

  • turkish lawyer

    3/12/2015 8:56:45 AM |

    The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it'll do even better in those areas, but for now it's a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod's strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.

  • Granite countertops kansas city

    3/13/2015 3:58:52 PM |

    Muchos Gracias for your blog post.Thanks Again. Really Great.

  • My Website 2015

    3/15/2015 4:50:07 AM |

    Really informative blog post.Really looking forward to read more. Keep writing.

  • Gary Lazeo

    3/17/2015 8:02:13 AM |

    Thank you for your article post.Thanks Again. Much obliged.

  • sewa mobil surabaya

    3/17/2015 11:33:37 AM |

    Thanks a lot for the article post.Thanks Again. Awesome.

  • eebest8 Mitch

    3/18/2015 3:00:15 AM |

    Rattling superb info can be found on website.

  • official site

    3/18/2015 12:25:37 PM |

    I simply want to tell you that I am just newbie to blogging and definitely loved you're page. Most likely I’m likely to bookmark your blog post . You absolutely have superb articles and reviews. Kudos for sharing your webpage.

  • eebest8 mitch

    3/18/2015 4:08:17 PM |

    pretty helpful material, overall I believe this is worthy of a bookmark, thanks

  • freshouse

    3/26/2015 6:27:35 PM |

    excellent issues altogether, you simply won a emblem new reader. What might you recommend about your submit that you just made some days in the past? Any certain?

  • web directory

    4/6/2015 1:54:29 AM |

    I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

Pingbacks and trackbacks (1)+

Comments are closed