Projecting results from MongoDb Find in F#

人走茶凉 提交于 2021-01-28 21:50:56

问题


I'm trying to query MongoDB using MongoDB.Driver and return a partial result using mongo projection.

I've figured out how to query without projection, but not a partial result. This is my function for finding results:

let Find<'a> collectionName (filterDefinition: FilterDefinition<'a>) =
    let collection = database.GetCollection<'a> collectionName
    collection.Find(filterDefinition).ToEnumerable() |> List.ofSeq

This is an example of how I call it:

let findByIdFilter id =
    Builders<MyModel>.Filter.Eq((fun s -> s.id), id)

let results = Find collectionName (findByIdFilter id)

Let's say my model is something like that:

type MyInnerModel = {
    zInner: bool
}

type MyModel = {
    id: ObjectId
    x: int
    y: double
    z: MyInnerModel
}

And these are my projections:

type MyModelProjection1 = {
    id: ObjectId
    y: double
}

type MyModelProjection1 = {
    id: ObjectId
    x: int
    z: MyInnerModel
}

How do I construct my queries for following scenarios:

  • Find documents matching (fun (m: MyModel) -> m.z.zInner = false) with projection to MyModelProjection1
  • Find documents matching (fun (m: MyModel) -> m.x = 5) with projection to MyModelProjection2
  • Find ALL documents with projection to MyModelProjection1 or MyModelProjection2

回答1:


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


来源:https://stackoverflow.com/questions/64723616/projecting-results-from-mongodb-find-in-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!