BSON Type Provider
This article demonstrates how to use the BSON type provider to access
.bson
files in a statically typed way.
The BSON type provider provides statically typed access to BsonDocument
s.
It takes as input a sequence of sample documents (e.g. the output from the
mongodump
utility). The generated type can then be used to read files
with the same structure. If the loaded file does not match the structure of
the samples, then a runtime error may occur (e.g. when accessing a
nonexistent field).
Introducing the provider
The type provider is located in the FSharp.Data.Bson.dll
assembly.
Assuming the assembly is located in the ../../../bin
directory, we can
load it in F# Interactive as follows:
1: 2: 3: 4: 5: 6: |
#I "../../../bin" #r "FSharp.Data.Bson.Runtime.dll" #r "FSharp.Data.Bson.dll" #r "MongoDB.Bson.dll" open BsonProvider |
Inferring a type from the samples
The BsonProvider<...>
takes a string
as its first static parameter,
representing the path to a file containing BSON. An absolute path can be
specified, or a path relative to the current working directory.
The following loads some zip code data using the provider:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: |
type ZipCode = BsonProvider<"../data/zipcodes.bson"> let zip0 = ZipCode.GetSamples().[0] zip0.Id |> ignore zip0.City |> ignore zip0.Loc |> ignore zip0.Pop |> ignore zip0.State |> ignore zip0.BsonValue |> ignore |
The generated type has multiple properties:
Id
of typestring
, corresponding to the_id
fieldCity
of typestring
, corresponding to thecity
fieldLoc
of typefloat[]
, corresponding to theloc
fieldPop
of typeint
, corresponding to thepop
fieldState
of typestring
, corresponding to thestate
fieldBsonValue
of typeBsonValue
, corresponding to the underlyingBsonDocument
The provider successfully infers the type from the samples, and exposes the various fields as properties (using a PascalCase name to follow standard .NET naming conventions).
Inferring a record type
The fields need not be of a primitive type for the inference to work.
In the case of a field containing an array of documents, a unifying type
for the BsonArray
is generated. If a certain property is missing from
one document, but present in another, then it is inferred as optional.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: |
type Student = BsonProvider<"../data/students.bson"> let (|Homework|_|) (score:Student.Score) = match score.Type with | "homework" -> Some score.Score | _ -> None let homeworkAvgs = Student.GetSamples() |> Seq.ofArray |> Seq.map (fun student -> student.Scores) |> Seq.map (Array.choose (|Homework|_|)) |> Seq.filter (fun homeworks -> homeworks.Length > 0) |> Seq.map (Array.average) |> Seq.toArray |
Summary
This article demonstrated the BsonProvider
type. The provider infers
the structure of a .bson
file and exposes it to F# programmers in a
nicely typed way.
Full name: BsonProvider.ZipCode
namespace BsonProvider
--------------------
type BsonProvider
Full name: BsonProvider.BsonProvider
<summary>
Typed representation of a BSON document.
</summary>
<param name='Path'>
Location of a BSON sample file.
</param>
<param name='InferLimit'>
Number of documents to use for inference. Defaults to 100.
If this is set as zero, then all documents are used.
</param>
<param name='ResolutionFolder'>
Directory used for resolving relative file references
(at design time and in hosted execution).
</param>
Full name: BsonProvider.zip0
Returns the entire set of sample BSON documents
Full name: Microsoft.FSharp.Core.Operators.ignore
Full name: BsonProvider.Student
inherit IBsonTop
new : bsonValue: BsonValue -> Score + 1 overload
member BsonValue : BsonValue
member Score : float
member Type : string
Full name: BsonProvider.BsonProvider,Path="../data/students.bson".Score
Full name: BsonProvider.homeworkAvgs
from Microsoft.FSharp.Collections
Full name: Microsoft.FSharp.Collections.Seq.ofArray
Full name: Microsoft.FSharp.Collections.Seq.map
from Microsoft.FSharp.Collections
Full name: Microsoft.FSharp.Collections.Array.choose
Full name: BsonProvider.( |Homework|_| )
Full name: Microsoft.FSharp.Collections.Seq.filter
Full name: Microsoft.FSharp.Collections.Array.average
Full name: Microsoft.FSharp.Collections.Seq.toArray