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, 感谢原作者分享。

INNODBは、レドログと非論的なものを使用して、データの一貫性と信頼性を確保しています。 1.レドログは、クラッシュの回復とトランザクションの持続性を確保するために、データページの変更を記録します。 2.Undologsは、元のデータ値を記録し、トランザクションロールバックとMVCCをサポートします。

説明コマンドのキーメトリックには、タイプ、キー、行、および追加が含まれます。 1)タイプは、クエリのアクセスタイプを反映しています。値が高いほど、constなどの効率が高くなります。 2)キーは使用されているインデックスを表示し、nullはインデックスがないことを示します。 3)行はスキャンされた行の数を推定し、クエリのパフォーマンスに影響します。 4)追加の情報を最適化する必要があるというFilesortプロンプトを使用するなど、追加情報を提供します。

Temporaryを使用すると、MySQLクエリに一時テーブルを作成する必要があることが示されています。これは、異なる列、またはインデックスされていない列を使用して順番に一般的に見られます。インデックスの発生を回避し、クエリを書き直し、クエリのパフォーマンスを改善できます。具体的には、expliect出力に使用を使用する場合、MySQLがクエリを処理するために一時テーブルを作成する必要があることを意味します。これは通常、次の場合に発生します。1)個別またはグループビーを使用する場合の重複排除またはグループ化。 2)Orderbyに非インデックス列が含まれているときに並べ替えます。 3)複雑なサブクエリを使用するか、操作に参加します。最適化方法には以下が含まれます。1)OrderbyとGroupB

MySQL/INNODBは、4つのトランザクション分離レベルをサポートしています。 1.ReadunCommittedは、知らないデータを読み取ることができます。 2。読み込みは汚い読み取りを回避しますが、繰り返しのない読みが発生する可能性があります。 3. RepeatablerEadはデフォルトレベルであり、汚い読み取りと非回復不可能な読みを避けますが、幻の読み取りが発生する可能性があります。 4. Serializableはすべての並行性の問題を回避しますが、同時性を低下させます。適切な分離レベルを選択するには、データの一貫性とパフォーマンス要件のバランスをとる必要があります。

MySQLは、Webアプリケーションやコンテンツ管理システムに適しており、オープンソース、高性能、使いやすさに人気があります。 1)PostgreSQLと比較して、MySQLは簡単なクエリと高い同時読み取り操作でパフォーマンスが向上します。 2)Oracleと比較して、MySQLは、オープンソースと低コストのため、中小企業の間でより一般的です。 3)Microsoft SQL Serverと比較して、MySQLはクロスプラットフォームアプリケーションにより適しています。 4)MongoDBとは異なり、MySQLは構造化されたデータおよびトランザクション処理により適しています。

MySQLインデックスのカーディナリティは、クエリパフォーマンスに大きな影響を及ぼします。1。高いカーディナリティインデックスは、データ範囲をより効果的に狭め、クエリ効率を向上させることができます。 2。低カーディナリティインデックスは、完全なテーブルスキャンにつながり、クエリのパフォーマンスを削減する可能性があります。 3。ジョイントインデックスでは、クエリを最適化するために、高いカーディナリティシーケンスを前に配置する必要があります。

MySQL学習パスには、基本的な知識、コアの概念、使用例、最適化手法が含まれます。 1)テーブル、行、列、SQLクエリなどの基本概念を理解します。 2)MySQLの定義、作業原則、および利点を学びます。 3)インデックスやストアドプロシージャなどの基本的なCRUD操作と高度な使用法をマスターします。 4)インデックスの合理的な使用や最適化クエリなど、一般的なエラーのデバッグとパフォーマンス最適化の提案に精通しています。これらの手順を通じて、MySQLの使用と最適化を完全に把握できます。

MySQLの実際のアプリケーションには、基本的なデータベース設計と複雑なクエリの最適化が含まれます。 1)基本的な使用法:ユーザー情報の挿入、クエリ、更新、削除など、ユーザーデータの保存と管理に使用されます。 2)高度な使用法:eコマースプラットフォームの注文や在庫管理など、複雑なビジネスロジックを処理します。 3)パフォーマンスの最適化:インデックス、パーティションテーブル、クエリキャッシュを使用して合理的にパフォーマンスを向上させます。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

ドリームウィーバー CS6
ビジュアル Web 開発ツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

WebStorm Mac版
便利なJavaScript開発ツール
