Skip to content

fsprojects/FSharp.MongoDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

3161543 · Jan 5, 2025
Dec 29, 2024
Jan 5, 2025
Dec 29, 2024
Jan 3, 2016
Dec 28, 2024
Dec 28, 2024
Aug 28, 2013
Dec 29, 2024
Jan 5, 2025
Feb 14, 2016

Repository files navigation

FSharp.MongoDB

Add support for F# types to .net MongoDB.Driver.

Goals of this project

  • Provide support for F# types for interacting with MongoDB.
  • Have an implementation that is fully testable without connecting to a server.
  • Isomorphic bson serialization for C# and F#.

FSharp.MongoDB history

Repository origins are:

  1. Initial repository: https://github.com/mongodb-labs/mongo-fsharp-driver-prototype
  2. Fork by @visemet (Max Hirschhorn): https://github.com/visemet/FSharp.MongoDB
  3. This repository: migrated to netstandard 2.1, nullable, adds new features (C#/F# isomomorphic serialization, voption support...)

Building

  • build using the top-level solution file (FSharp.MongoDB.sln).
  • you can use make with target build or test.

Supported platforms

This project targets netstandard2.1 (compatible runtimes).

Contributing

  • If you have a question about the library, then create an issue with the question label.
  • If you'd like to report a bug or submit a feature request, then create an issue with the appropriate label.
  • If you'd like to contribute, then feel free to send a pull request.

Usage

Installation

To serialize F# types, first install this project via NuGet:

Package Status Description
FSharp.MongoDB Nuget Add F# support to MongoDB.Driver

Register

On startup you have to register serializers:

MongoDB.Bson.Serialization.FSharp.register()

Serialization format

List

List<_> is serialized as an array. Order is preserved.

Set

Set<_> is serialized as an array. Do not rely on the order.

Array

List<_> is serialized as an array. Order is preserved.

Seq

Seq<_> is serialized as an array. Order is preserved.

Map

Map<_, _> is serialized as an object. Do not rely on the order of the keys.

Option

Option<_> is either serialized as:

  • null if Option.None
  • object if Option.Some

On deserialization, missing value is mapped to None.

ValueOption

ValueOption<_> is either serialized as:

  • null if ValueOption.ValueNone
  • object if ValueOption.ValueSome

On deserialization, missing value is mapped to ValueNone.

Record

A record is serialized as an `object.

If you want to auto-generate ObjectId (as the Id of the collection), add [<CLIMutable>] on the record.

Discriminated union

The case of the discriminated union is stored in _t key. Each value of the DU is serialized as an object using its corresponding value name.

Nullable Reference Type

NRT are serialized as:

  • null if Null
  • object if NonNull object

⚠️ As of now, NRT can't be considered null when deserializing if value is missing.

License

The contents of this library are made available under the Apache License, Version 2.0.

Build Status

Build status