Projecting results from MongoDb Find in F#

You can define your projections like so:

let projection1 =
    Builders<MyModel>.Projection.Expression(fun model ->
        { id = model.id; y = model.y })

let projection2 =
    Builders<MyModel>.Projection.Expression(fun model ->
        { id = model.id; x = model.x; z = model.z })

You can then use them in the following way:

    let one =
        collection
            .Find(fun m -> m.z.zInner = false)
            .Project(projection1).ToEnumerable() |> List.ofSeq

    let two =
        collection
            .Find(fun m -> m.x = 5)
            .Project(projection2).ToEnumerable() |> List.ofSeq

    let three =
        collection
            .Find(fun _ -> true)
            .Project(projection1).ToEnumerable() |> List.ofSeq

    let four =
        collection
            .Find(fun _ -> true)
            .Project(projection1).ToEnumerable() |> List.ofSeq

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top