Enhancing the F# developer experience with MongoDB
This is a guest post by Max Hirschhorn,who is currently an intern at MongoDB. About the F# programming language F# is a multi-paradigm language built on the .NET framework. It isfunctional-first and prefers immutability, but also supportso
This is a guest post by Max Hirschhorn, who is currently an intern at MongoDB.
About the F# programming language
F# is a multi-paradigm language built on the .NET framework. It is functional-first and prefers immutability, but also supports object-oriented and imperative programming styles.
Also, F# is a statically-typed language with a type inference system. It has a syntax similar to Ocaml, and draws upon ideas from other functional programming languages such as Erlang and Haskell.
Using the existing .NET driver
The existing .NET driver is compatible with F#, but is not necessarily written in a way that is idiomatic to use from F#.
Part of the reason behind this is that everything in F# is explicit. For example, consider the following example interface and implementing class.
[] type I = abstract Foo : unit -> string type C() = interface I with member __.Foo () = "bar" // example usage let c = C() (c :> I).Foo()
So in order to use any of the interface members, the class must be
upcasted using
the :>
operator. Note that this cast is still checked at compile-time.
In a similar vein, C# supports implicit
operators,
which the BSON library uses for converting between a primitive value
and its BsonValue
equivalent, e.g.
new BsonDocument { { "price", 1.99 }, { "$or", new BsonDocument { { "qty", new BsonDocument { { "$lt", 20 } } }, { "sale", true } } } };
whereas F# does not. This requires the developer to explicitly
construct the appropriate type of BsonValue
, e.g.
BsonDocument([ BsonElement("price", BsonDouble(1.99)) BsonElement("$or", BsonArray([ BsonDocument("qty", BsonDocument("$lt", BsonInt32(20))) BsonDocument("sale", BsonBoolean(true)) ])) ])
with the query builder, we can hide the construction of BsonDocument
instances, e.g.
Query.And([ Query.EQ("price", BsonDouble(1.99)) Query.OR([ Query.LT("qty", BsonInt32(20)) Query.EQ("sale", BsonBoolean(true)) ]) ])
It is worth noting that the need to construct the BsonValue
instances
is completely avoided when using a typed QueryBuilder
.
type Item = { Price : float Quantity : int Sale : bool } let query = QueryBuilder() query.And([ query.EQ((fun item -> item.Price), 1.99) query.Or([ query.LT((fun item -> item.Quantity), 20) query.EQ((fun item -> item.Sale), true) ]) ])
What we are looking for is a solution that matches the brevity of F# code, offers type-safety if desired, and is easy to use from the language.
New features
The main focus of this project is to make writing queries against MongoDB as natural from the F# language as possible.
bson
quotations
We strive to make writing predicates as natural as possible by reusing as many of the existing operators as possible.
A taste
Consider the following query
{ price: 1.99, $or: [ { qty: { $lt: 20 } }, { sale: true } ] }
we could express this with a code quotation
bson x?price = 1.99 && (x?qty
or with type safety
bson x.Price = 1.99 && (x.Quantity
Breaking it down
The quotations are not actually executed, but instead are presented
as an abstract syntax tree (AST), from which an equivalent
BsonDocument
instance is constructed.
The ?
operator
The ?
operator is defined to allow for an unchecked comparison. The
F# language supports the ability to do a dynamic lookup (get) and
assignment (set) via the ?
and ? operators respectively, but does
not actually provide a implementation.
So, the F# driver defines the ?
operator as the value associated with
a field in a document casted to a fresh generic type.
// type signature: BsonDocument -> string -> 'a let (?) (doc : BsonDocument) (field : string) = unbox doc.[field]
and similarly defines the ? operator as the coerced assignment of a
generically typed value to the associated field in the document.
// type signature: BsonDocument -> string -> 'a -> unit let (? ignore
Queries
Unchecked expressions have the type signature
Expr<bsondocument> bool></bsondocument>
.
// $mod bson x?qty % 4 = 0 @>
Checked expressions have the type signature Expr bool>
.
// $mod bson x.Quantity % 4 = 0 @>
Updates
Unchecked expressions have the type signature
Expr<bsondocument> unit list></bsondocument>
. The reason for the list
in the
return type is to perform multiple update operations.
// $set bson [ x?qty // $inc bson [ x?qty
Mmm… sugar
A keen observer would notice that (+) 1
is not an int
, but actually
a function int -> int
. We are abusing the fact that type safety is
not enforced here by assigning the quantity field of the document to a
lambda expression, that takes a single parameter of the current value.
Note that
// $inc bson [ x?qty
is also valid.
Checked expressions either have the type signature
Expr unit list>
or Expr 'DocType>
,
depending on whether the document type has mutable fields (only matters
for record types).
// $set bson [ x.Quantity // $inc bson [ x.Quantity
mongo
expressions
Uses the monadic structure (computation expression) to define a pipeline of operations that are executed on each document in the collection.
Queries
let collection : IMongoCollection = ... mongo { for x in collection do where (x?price = 1.99 && (x?qty <p>or with a typed collection</p> <pre class="brush:php;toolbar:false"> let collection : IMongoCollection = ... mongo { for x in collection do where (x.price = 1.99 && (x.qty <h5 id="Updates">Updates</h5> <pre class="brush:php;toolbar:false"> let collection : IMongoCollection = ... mongo { for x in collection do update set x?price 0.99 inc x?qty 1 }
or with a typed collection
let collection : IMongoCollection = ... mongo { for x in collection do update set x.Price 0.99 inc x.Quantity 1 }
Serialization of F# data types
Now supports
- record types
- option types
- discriminated unions
Conclusion
Resources
The source code is available at GitHub. We absolutely encourage you to experiment with it and provide us feedback on the API, design, and implementation. Bug reports and suggestions for improvements are welcomed, as are pull requests.
Disclaimer. The API and implementation are currently subject to change at any time. You must not use this driver in production, as it is still under development and is in no way supported by MongoDB, Inc.
Acknowledgments
Many thanks to the guidance from the F# community on Twitter, and my mentors: Sridhar Nanjundeswaran, Craig Wilson, and Robert Stam. Also, a special thanks to Stacy Ferranti and Ian Whalen for overseeing the internship program.
原文地址:Enhancing the F# developer experience with MongoDB, 感谢原作者分享。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

機器之能報道編輯:吳昕國內版的人形機器人+大模型組隊,首次完成疊衣服這類複雜柔性材料的操作任務。隨著融合了OpenAI多模態大模型的Figure01揭開神秘面紗,國內同行的相關進展一直備受關注。就在昨天,國內"人形機器人第一股"優必選發布了人形機器人WalkerS深入融合百度文心大模型後的首個Demo,展示了一些有趣的新功能。現在,得到百度文心大模型能力加持的WalkerS是這個樣子的。和Figure01一樣,WalkerS沒有走動,而是站在桌子後面完成一系列任務。它可以聽從人類的命令,折疊衣物

THE是什麼幣種?THE(TokenizedHealthcareEcosystem)是一種數位貨幣,利用區塊鏈技術,專注於醫療健康產業的創新和改革。 THE幣的使命是利用區塊鏈技術來提高醫療產業的效率和透明度,推動各方之間更有效率的合作,包括病患、醫護人員、製藥公司和醫療機構。 THE幣的價值和特徵首先,THE幣作為一種數位貨幣,具備了區塊鏈的優勢——去中心化、安全性高、交易透明等,讓參與者能夠信任和依賴這個系統。其次,THE幣的獨特之處在於它專注於醫療健康產業,並藉助區塊鏈技術改造了傳統醫療體系,提升了

如何查詢TheSandbox幣最新價格TheSandbox是建立在以太坊區塊鏈上的去中心化遊戲平台,使用其原生代幣SAND可以購買土地、資產和遊戲體驗。想要查詢SAND最新價格的步驟如下:選擇一個可靠的價格查詢網站或應用程式。一些常用的價格查詢網站包括:CoinMarketCap:https://coinmarketcap.com/Coindesk:https://www.coindesk.com/幣安:https://www.binance.com/在網站或應用程式中搜尋SAND。查看SAND

三星計劃在今年下半年推出新一代GalaxyZFold與Flip6系列折疊螢幕智慧型手機。近期,韓國媒體TheElec和《時事周刊e》透露了關於這兩款新品的更多細節。三星GalazyZFold6爆料圖片。圖源@chunvn8888根據TheElec報道,三星電子的供應鏈廠商預計於5月初啟動GalaxyZFold6和Flip6相關組件的生產工作,相較之下,去年GalaxyZFold5和Flip5的零件生產始於5月下半月。這意味著今年的標準版GalaxyZ系列發佈時間表相較於上年提前了約兩週至三週的時間。去

如何查詢TheGraph幣最新價格? TheGraph是一個去中心化的協議,旨在為區塊鏈資料提供高效的索引和查詢服務。該協議的設計使得開發人員能夠更輕鬆地建立和推出分散式應用程式(dApp),並讓這些應用程式能夠方便地存取區塊鏈資料。要查詢TheGraph幣(GRT)的最新價格,您可以按照以下步驟操作:選擇一個可靠的價格查詢網站或應用程式。一些常用的價格查詢網站包括:CoinMarketCap:https://coinmarketcap.com/Coindesk:https://www.coind

微軟日前發布了一款名為DeveloperProxy的命令列工具,目前已更新至0.9預覽版本,該軟體可用於測試軟體呼叫HTTPAPI的行為,避免開發者向使用者請求過多權限,可預防軟體的過度授權問題。 DeveloperProxy工具能夠抓取應用程式所發出的一系列MicrosoftGraphAPI請求,並且可以自動偵測應用程式所需呼叫的API數量。微軟表示,這些權限比較工作會在本地端進行,因此使用者的資料不會上傳到外界。當工具偵測到應用程式擁有的權限,超過應用程式實際需要的權限時,便會向開發者發出相關警告,此時

如何查看TheGraph幣市值TheGraph是一種去中心化的協議,旨在幫助開發者索引和查詢區塊鏈資料。其代幣GRT被用於支付網路費用和獎勵節點營運商。查看TheGraph幣市值的方法:選擇一個可靠的網站或平台:有多個網站和平台提供加密貨幣市值信息,例如CoinMarketCap、CoinGecko、非小號等。選擇一個可靠的網站或平台很重要,以確保您獲得準確的資訊。搜尋TheGraph:在網站或平台上搜尋GRT或TheGraph。查看市值:TheGraph的市值通常會在搜尋結果中顯示。提示:市值

最近读了罗技在上半年出品的企业桌面配置白皮书,涉及到的企业级桌面外设的知识面和选购逻辑,给了我们非常多的启发。其中许多新鲜的观点,非常适合与中关村的老粉们分享。罗技白皮书:桌面外设采购新思考罗技作为桌面外设领域的佼佼者,其品牌实力和技术创新有目共睹。白皮书发布时间的意义罗技白皮书的发布时机恰逢企业办公模式转型之际。混合办公模式的普及对雇主品牌和人才吸引提出了新挑战。桌面外设采购新趋势以往的桌面外设采购标准可能过于简化。不同岗位员工对键盘、鼠标、耳机和摄像头的需求差异显著。罗技白皮书中的观点罗技白
