MongoDB driver - an alternative MongoDB driver

Announcing an alternative MongoDB Driver! Do you like the MongoDB despite the general opinion a database always has to be an sql database? Would you like to use your favorite database with your favorite language? Then you are right here!

Sadly there is no official driver (I do not know why?). So you have to take things into your own hands and write the driver yourself. Currently the implementation does not support all features yet. But over time (hopefully) the succesive expansion of the missing features will take place.

Features:

  • Supports MongoDB versions 3.2, 3.4, 3.6, 4.0
  • Connection pooling (through DBConnection 2.x)
  • Streaming cursors
  • Performant ObjectID generation
  • Follows driver specification set by 10gen
  • Safe (by default) and unsafe writes
  • Aggregation pipeline
  • Replica sets
  • Support for SCRAM-SHA-256 (MongoDB 4.x)
  • Support for change streams api (See)

More information, sources and packages:
github hex.pm

10 Likes

Welcome to the forum, and thank you for sharing your hard work on this library!

From the GitHub Readme, it gather that mongodb_driver is a split-off with some different design decisions from the mongodb library.
Do you know how it compares to the other MongoDB-drivers that currently are floating around in the Elixir ecosystem? (mongo, mongodb_erlang, mongodb_client and mongox)

2 Likes

let me say: tempus fugit.

mongox - DEPRECATED
mongo - 0.5.4 December 1, 2015 (docs)
mongodb_erlang - seems not to support op_msg (since 3.6)

Unfortunately, the libraries are not up-to-date regarding the MongoDB server. Since I use the latest version of my projects and therefore want to use the features of the server, I was looking for a suitable driver. Unfortunately I did not find one, so I searched for the best starting point and developed my own driver.

4 Likes

The new version 0.6.0 has been released. Additional features since last announcing (May 4):

  • Support for bulk writes (See specs)

  • support for driver sessions (See specs)

  • support driver transactions (See specs)

  • Enhancements

    • refactored writeConcern
    • refactored filter_nils
    • refactored usage of ReadPreference
    • added support for sessions ( ServerSession , SessionPool , Session )
    • added Decimal128 encoder
    • added support for transactions
    • added support for transaction to gridfs and bulk operation
    • added create command (explicitly creating a collection or view)
    • Supports MongoDB versions 3.2, 3.4, 3.6, 4.0, 4.2

Check the documentation for more information:

7 Likes

I don’t use MongoDB at present. But, just wanted to say, nice work @zookzook. I hope you keep getting traction with this.

2 Likes

Thank you for your feedback! I am always happy when I get some feedback

The new version 0.7.0 has been released:

  • support for command monitoring
  • support for retryable reads
  • support for retryable writes

Enhancements:

  • refactored the api of Mongo.limits and Mongo.wire_version
  • add support for tracking recovery token from response in a sharded transaction
  • new function BSON.ObjectId.decode/1 and BSON.ObjectId.encode/1
  • new function Mongo.uuid/1
  • refactored event notification system
  • refactored the test cases
  • now using mtools for a MongoDB deployment in the travis ci environment
  • Session.commit_transaction returns now :ok or an error {:error, %Mongo.Error{}}
  • support for errorLabels
4 Likes

Announcing a helper library to support using structs instead of bare maps while using the MongoDB-Driver:

Hex.pm
Github
Demo

Features:

  • automatic load and dump function
  • reflection functions
  • type specification
  • support for embedding one and many structs
  • support for after load function
  • support for before dump function
  • support for id generation
  • support for default values
  • support for derived values

The module creates some boilerplate code, so you can use structs for your data. You call load/1 to convert the map from the driver to your struct. To save your struct to MongoDB you call dump/1 before you call insert_one or insert_many.

Check out the docs!

7 Likes

Actually I’m using your driver in production and works well, I have a filtering question that I posted here.


If you’re able to give it a look

thanks

See my answer here.

The driver is now OTP 24 ready. Thanks to aenglisc for replacing the deprecated functions.

Hello, I was wondering If this supported latest mongo version ? such as 4.2 or 4.4 ?

I guess both versions are supported.

The new version 0.8.0 has been released.

  • supports MongoDB versions 3.2, 3.4, 3.6, 4.x, 5.x
  • replica set connection: faster topology update if the primary is down (thanks to p-mongo)
  • added custom Mongo.Encoder protocol (thanks to esse)
  • added collection from yildun project
  • fixed an issue that the bulk operation does not stop after any insert/update/delete failed (thanks to ja-jimenez)
1 Like

How to use aggregation in your driver?

i tried this

Mongo.aggregate(:mongo, "customer", %{"$group" => %{ "_id" => "$cust_id", "total" => %{ "$sum" => "$amount"}}})

but it returns following struct instead of actual records.

%Mongo.Cursor{
  cmd: [
    aggregate: "customer",
    pipeline: %{
      "$group" => %{"_id" => "$cust_id", "total" => %{"$sum" => "$amount"}}
    },
    cursor: %{}
  ],
  on_resume_token: nil,
  opts: [],
  topology_pid: :mongo
}

The result is a cursor. You can stream or “enum” the cursor like this:

cursor = Mongo.aggregate(:mongo, "customer", %{"$group" => %{ "_id" => "$cust_id", "total" => %{ "$sum" => "$amount"}}})

cursor
|> Enum.to_list()
|> IO.inspect

but i got the following error

# 1
        {:error,
         %Mongo.Error{
           code: 14,
           host: nil,
           message: "'pipeline' option must be specified as an array"
         }}

but this solved it wrapping the group statement inside an array solved it