Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
F# MongoDB sample (C# Driver)
#I @"..\packages\mongocsharpdriver.1.8.3\lib\net35\"
#r "MongoDB.Driver.dll"
#r "MongoDB.Bson.dll"
open System
open MongoDB.Bson
open MongoDB.Bson.Serialization.Attributes
open MongoDB.Driver
open MongoDB.Driver.Builders
open MongoDB.Driver.Linq
type Tweet() =
member val Id = ObjectId.Empty with get, set
member val ProfileImageLocation = "" with get, set
[<BsonDateTimeOptions(Kind = DateTimeKind.Local)>]
member val CreatedDate = DateTime.MinValue with get, set
member val FromUserDisplayName = "" with get, set
member val FromUserScreenName = "" with get, set
member val ToUserDisplayName = "" with get, set
member val ToUserScreenName = "" with get, set
member val Text = "" with get, set
member val TweetId = "" with get, set
member val Source = "" with get, set
member val Language = "" with get, set
member val Location = "" with get, set
let tweets =
let collectionName = "fsharp"
let client = MongoClient(@"mongodb://localhost")
let server = client.GetServer()
let database = server.GetDatabase("fsharp")
let _collection = database.GetCollection<Tweet>(collectionName);
let collection =
if (_collection.Exists()) then _collection
else database.CreateCollection(collectionName) |> ignore
query {
for x in collection.AsQueryable<Tweet>() do
select x }
|> Seq.toArray
Copy link

DRouh commented Mar 11, 2017

Despite this gist being useful as it is, I'd like to provide a more or less useful comment (for those who only starting with MongoDB) regarding this class construct. It is not longer necessary and can easily be replaced with a plain F# record type with [].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment