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

Innodbbufferpool mengurangkan cakera I/O dengan data caching dan halaman pengindeksan, meningkatkan prestasi pangkalan data. Prinsip kerjanya termasuk: 1. Bacaan Data: Baca data dari Bufferpool; 2. Penulisan Data: Selepas mengubah suai data, tulis kepada Bufferpool dan menyegarkannya ke cakera secara teratur; 3. Pengurusan cache: Gunakan algoritma LRU untuk menguruskan halaman cache; 4. Mekanisme Membaca: Muatkan halaman data bersebelahan terlebih dahulu. Dengan saiz bufferpool dan menggunakan pelbagai contoh, prestasi pangkalan data dapat dioptimumkan.

Berbanding dengan bahasa pengaturcaraan lain, MySQL digunakan terutamanya untuk menyimpan dan mengurus data, manakala bahasa lain seperti Python, Java, dan C digunakan untuk pemprosesan logik dan pembangunan aplikasi. MySQL terkenal dengan prestasi tinggi, skalabilitas dan sokongan silang platform, sesuai untuk keperluan pengurusan data, sementara bahasa lain mempunyai kelebihan dalam bidang masing-masing seperti analisis data, aplikasi perusahaan, dan pengaturcaraan sistem.

MySQL bernilai belajar kerana ia adalah sistem pengurusan pangkalan data sumber terbuka yang sesuai untuk penyimpanan data, pengurusan dan analisis. 1) MySQL adalah pangkalan data relasi yang menggunakan SQL untuk mengendalikan data dan sesuai untuk pengurusan data berstruktur. 2) Bahasa SQL adalah kunci untuk berinteraksi dengan MySQL dan menyokong operasi CRUD. 3) Prinsip kerja MySQL termasuk seni bina klien/pelayan, enjin penyimpanan dan pengoptimum pertanyaan. 4) Penggunaan asas termasuk membuat pangkalan data dan jadual, dan penggunaan lanjutan melibatkan menyertai jadual menggunakan Join. 5) Kesilapan umum termasuk kesilapan sintaks dan isu kebenaran, dan kemahiran debugging termasuk menyemak sintaks dan menggunakan perintah menjelaskan. 6) Pengoptimuman prestasi melibatkan penggunaan indeks, pengoptimuman penyata SQL dan penyelenggaraan pangkalan data yang tetap.

MySQL sesuai untuk pemula untuk mempelajari kemahiran pangkalan data. 1. Pasang alat pelayan dan klien MySQL. 2. Memahami pertanyaan SQL asas, seperti SELECT. 3. Operasi data induk: Buat jadual, masukkan, kemas kini, dan padam data. 4. Belajar Kemahiran Lanjutan: Fungsi Subquery dan Window. 5. Debugging dan Pengoptimuman: Semak sintaks, gunakan indeks, elakkan pilih*, dan gunakan had.

MySQL dengan cekap menguruskan data berstruktur melalui struktur jadual dan pertanyaan SQL, dan melaksanakan hubungan antara meja melalui kunci asing. 1. Tentukan format data dan taip apabila membuat jadual. 2. Gunakan kunci asing untuk mewujudkan hubungan antara jadual. 3. Meningkatkan prestasi melalui pengindeksan dan pengoptimuman pertanyaan. 4. Secara kerap sandaran dan memantau pangkalan data untuk memastikan pengoptimuman keselamatan data dan prestasi.

MySQL adalah sistem pengurusan pangkalan data sumber terbuka yang digunakan secara meluas dalam pembangunan web. Ciri -ciri utamanya termasuk: 1. Menyokong pelbagai enjin penyimpanan, seperti InnoDB dan Myisam, sesuai untuk senario yang berbeza; 2. Menyediakan fungsi replikasi master-hamba untuk memudahkan pengimbangan beban dan sandaran data; 3. Meningkatkan kecekapan pertanyaan melalui pengoptimuman pertanyaan dan penggunaan indeks.

SQL digunakan untuk berinteraksi dengan pangkalan data MySQL untuk merealisasikan penambahan data, penghapusan, pengubahsuaian, pemeriksaan dan reka bentuk pangkalan data. 1) SQL Melaksanakan operasi data melalui Pilih, Masukkan, Kemas kini, Padam Penyataan; 2) Gunakan pernyataan membuat, mengubah, drop untuk reka bentuk dan pengurusan pangkalan data; 3) Pertanyaan kompleks dan analisis data dilaksanakan melalui SQL untuk meningkatkan kecekapan membuat keputusan perniagaan.

Operasi asas MySQL termasuk membuat pangkalan data, jadual, dan menggunakan SQL untuk melakukan operasi CRUD pada data. 1. Buat pangkalan data: createdatabasemy_first_db; 2. Buat Jadual: CreateTableBooks (Idintauto_IncrementPrimaryKey, Titlevarchar (100) NotNull, Authorvarchar (100) NotNull, Published_yearint); 3. Masukkan Data: InsertIntoBooks (Tajuk, Pengarang, Published_year) VA


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SecLists
SecLists ialah rakan penguji keselamatan muktamad. Ia ialah koleksi pelbagai jenis senarai yang kerap digunakan semasa penilaian keselamatan, semuanya di satu tempat. SecLists membantu menjadikan ujian keselamatan lebih cekap dan produktif dengan menyediakan semua senarai yang mungkin diperlukan oleh penguji keselamatan dengan mudah. Jenis senarai termasuk nama pengguna, kata laluan, URL, muatan kabur, corak data sensitif, cangkerang web dan banyak lagi. Penguji hanya boleh menarik repositori ini ke mesin ujian baharu dan dia akan mempunyai akses kepada setiap jenis senarai yang dia perlukan.

PhpStorm versi Mac
Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).

Muat turun versi mac editor Atom
Editor sumber terbuka yang paling popular

ZendStudio 13.5.1 Mac
Persekitaran pembangunan bersepadu PHP yang berkuasa