• This repository has been archived on 19/Oct/2021
  • Stars
    star
    380
  • Rank 112,766 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 11 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This is superseded by the official MongoDB Rust Driver

This Repository is NOT a supported MongoDB product

Travis Crates.io docs.rs License

MongoDB Rust Driver Prototype

NOTE: This driver is superseded by the official MongoDB Rust driver, and will no longer be updated.

This branch contains active development on a new driver written for Rust 1.x and MongoDB 3.0.x.

The API and implementation are currently subject to change at any time. You should not use this driver in production as it is still under development and is in no way supported by MongoDB Inc. 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.

Note: This driver currently only supports MongoDB 3.0.x and 3.2.x. This driver is not expected to work with MongoDB 2.6 or any earlier versions. Do not use this driver if you need support for other versions of MongoDB.

Installation

Dependencies

Importing

The driver is available on crates.io. To use the MongoDB driver in your code, add the bson and mongodb packages to your Cargo.toml:

[dependencies]
mongodb = "0.3.11"

Alternately, you can use the MongoDB driver with SSL support. To do this, you must have OpenSSL installed on your system. Then, enable the ssl feature for MongoDB in your Cargo.toml:

[dependencies]
# ...
mongodb = { version = "0.3.11", features = ["ssl"] }

Then, import the bson and driver libraries within your code.

#[macro_use(bson, doc)]
extern crate mongodb;

or with Rust 2018:

extern crate mongodb;
use mongodb::{bson, doc};

Examples

Here's a basic example of driver usage:

use mongodb::{Bson, bson, doc};
use mongodb::{Client, ThreadedClient};
use mongodb::db::ThreadedDatabase;

fn main() {
    let client = Client::connect("localhost", 27017)
        .expect("Failed to initialize standalone client.");

    let coll = client.db("test").collection("movies");

    let doc = doc! {
        "title": "Jaws",
        "array": [ 1, 2, 3 ],
    };

    // Insert document into 'test.movies' collection
    coll.insert_one(doc.clone(), None)
        .ok().expect("Failed to insert document.");

    // Find the document and receive a cursor
    let mut cursor = coll.find(Some(doc.clone()), None)
        .ok().expect("Failed to execute find.");

    let item = cursor.next();

    // cursor.next() returns an Option<Result<Document>>
    match item {
        Some(Ok(doc)) => match doc.get("title") {
            Some(&Bson::String(ref title)) => println!("{}", title),
            _ => panic!("Expected title to be a string!"),
        },
        Some(Err(_)) => panic!("Failed to get next from server!"),
        None => panic!("Server returned no results!"),
    }
}

To connect with SSL, use either ClientOptions::with_ssl or ClientOptions::with_unauthenticated_ssl and then Client::connect_with_options. Afterwards, the client can be used as above (note that the server will have to be configured to accept SSL connections and that you'll have to generate your own keys and certificates):

use mongodb::{Bson, bson, doc};
use mongodb::{Client, ClientOptions, ThreadedClient};
use mongodb::db::ThreadedDatabase;

fn main() {
    // Path to file containing trusted server certificates.
    let ca_file = "path/to/ca.crt";
    // Path to file containing client certificate.
    let certificate = "path/to/client.crt";
    // Path to file containing the client private key.
    let key_file = "path/to/client.key";
    // Whether or not to verify that the server certificate is valid. Unless you're just testing out something locally, this should ALWAYS be true.
    let verify_peer = true;

    let options = ClientOptions::with_ssl(ca_file, certificate, key_file, verify_peer);

    let client = Client::connect_with_options("localhost", 27017, options)
        .expect("Failed to initialize standalone client.");

    let coll = client.db("test").collection("movies");

    let doc = doc! {
        "title": "Jaws",
        "array": [ 1, 2, 3 ],
    };

    // Insert document into 'test.movies' collection
    coll.insert_one(doc.clone(), None)
        .ok().expect("Failed to insert document.");

    ...
}

Testing

The driver test suite is largely composed of integration tests and behavioral unit-tests, relying on the official MongoDB specifications repo.

The easiest way to thoroughly test the driver is to set your fork up with TravisCI. However, if you'd rather test the driver locally, you'll need to setup integration and specification tests.

NOTE: Each integration test uses a unique database/collection to allow tests to be parallelized, and will drop their dependencies before running. However, effects are not cleaned up afterwards.

Setting up integration tests

All integration tests run on the default MongoDB port, 27017. Before running the tests, ensure that a test database is setup to listen on that port.

If you don't have mongodb installed, download and install a version from the MongoDB Download Center. You can see a full list of versions being tested on Travis in the travis config.

After installation, run a MongoDB server on 27017:

mkdir -p ./data/test_db
mongod --dbpath ./data/test_db

Setting up the specifications submodule

Pull in the specifications submodule at tests/json/data/specs.

git submodule update --init

Running Tests

Run tests like a regular Rust program:

cargo test --verbose

More Repositories

1

full-stack-fastapi-mongodb

Full stack, modern web application generator. Using FastAPI, MongoDB as database, Docker, automatic HTTPS and more.
TypeScript
473
star
2

sleepy.mongoose

[Archive] A REST interface for MongoDB - This Repository is NOT a supported MongoDB product
Python
385
star
3

socialite

Social Data Reference Architecture - This Repository is NOT a supported MongoDB product
Java
300
star
4

edda

A log visualizer for MongoDB - This Repository is NOT a supported MongoDB product
Python
231
star
5

mongo-perl-driver

Perl driver for the MongoDB
Perl
203
star
6

mongo-arrow

MongoDB integrations for Apache Arrow. Export MongoDB documents to numpy array, parquet files, and pandas dataframes in one line of code.
Python
85
star
7

hvdf

[Archive] High Volume Data Feed (HDVF) framework for time series data - This Repository is NOT a supported MongoDB product
Java
84
star
8

mongoproxy

A server that speaks the MongoDB wire protocol and can analyze/transform requests and responses - This Repository is NOT a supported MongoDB product
Go
79
star
9

mongo-web-shell

[Archive] An interactive MongoDB shell in a web browser - This Repository is NOT a supported MongoDB product
JavaScript
64
star
10

YCSB

This Repository is NOT a supported MongoDB product
Java
58
star
11

mongo-hhvm-driver-unsupported

[Archive] Experimental MongoDB driver for HHVM - This Repository is NOT a supported MongoDB product
Hack
56
star
12

mongorover

Intern project - MongoDB driver for the Lua programming language - This Repository is NOT a supported MongoDB product
C
55
star
13

big-data-exploration

[Archive] Intern project - Big Data Exploration using MongoDB - This Repository is NOT a supported MongoDB product
JavaScript
43
star
14

mongo-mockup-db

MockupDB - Simulate a MongoDB server.
Python
42
star
15

python-bsonjs

A fast BSON to MongoDB Extended JSON converter for Python - This Repository is NOT a supported MongoDB product
C
40
star
16

shard-viz

[Archive] An experimental Chrome plugin for visualizing sharded MongoDB clusters - This Repository is NOT a supported MongoDB product
JavaScript
36
star
17

py-tpcc

MongoDB Adaptation of PyTPCC
Python
34
star
18

mangrove

Mangrove: the MongoDB C++ ODM - This Repository is NOT a supported MongoDB product
C++
33
star
19

mongosh-snippets

An experimental plugin feature for mongosh
JavaScript
31
star
20

mongo-fsharp-driver-prototype

[Archive] FSharp.MongoDB - F# driver prototype for MongoDB - This Repository is NOT a supported MongoDB product
F#
29
star
21

storage-viz

[Archive] An experimental Chrome plugin for visualizing MMAP storage and indexes - This Repository is NOT a supported MongoDB product
JavaScript
24
star
22

mms-api-examples

This Repository is NOT a supported MongoDB product
Python
21
star
23

ledger

An implementation of a ledger using MongoDB
Python
18
star
24

mongo-csharp-search

C# driver extension providing support for Atlas Search
C#
16
star
25

migration-verifier

A public tool for validating the correctness of a migration
Go
14
star
26

mongoreplay

Go
13
star
27

field-level-encryption-sandbox

sample code for the client-side field level encryption project
JavaScript
13
star
28

jaeger-mongodb

A mongodb based storage backend for the jaeger collector
Go
12
star
29

pymongo-stubs

Experimental stub files for PyMongo
Python
12
star
30

mongodb-redpanda-example

JavaScript
11
star
31

drivers-evergreen-tools

Scripts for MongoDB drivers to bootstrap their Evergreen configuration file - This Repository is NOT a supported MongoDB product
Python
10
star
32

mindexer

Experimental tool to recommend indexes for MongoDB
Python
10
star
33

pubsub

[Archive] Intern project - Publish/subscribe within MongoDB v2.6.3 using ZeroMQ - This Repository is NOT a supported MongoDB product
C++
9
star
34

mongodb-wp-proxy

A logging mongodb wire protocol proxy
TypeScript
7
star
35

repl-trace-checker

Check that a replica set's execution trace is a valid TLA+ spec behavior
Python
6
star
36

symfony-mongodb

(Experimental) Bundle to provide a lightweight integration of MongoDB in Symfony
PHP
6
star
37

cubes

Creating OLAP Cubes with MongoDB's Aggregation Framework
JavaScript
6
star
38

drivers-atlas-testing

Drivers Atlas Testing
Python
5
star
39

agg_edsl

A simple eDSL for aggregation in Python
Python
5
star
40

benchmarks

Lua
4
star
41

cobra2snooty

Generate cobra docs compatible with the snooty docs tooling
Go
4
star
42

terraform-provider-mongodb

Terraform Provider for MongoDB Cloud resources (This repository is NOT a supported MongoDB product)
Go
4
star
43

mongo-perl-bson-xs

Perl XS implementation of the BSON Specification -- http://bsonspec.org
C
4
star
44

labs-modules

Labs modules for MongoDB.
Python
3
star
45

pymongoexplain

Explainable CRUD API for PyMongo
Python
3
star
46

omida

Ops Manager in Docker (arm64) WARNING: THIS IS NOT A PRODUCTION-GRADE IMAGE !!!
Shell
3
star
47

ai-ml-pipeline-testing

Repository Dedicated to testing our third-party AI/ML integrations
Shell
3
star
48

PyMongoAgg

Transpile Python functions to MongoDB Aggregations
Python
3
star
49

powerbi-connector-prototype

Prototype Custom Connector for PowerBI
3
star
50

ego

A non-production provisioning tool for installing MongoDB Ops Manager
Shell
3
star
51

swift-mongodb-tensorflow

Swift
2
star
52

mongodbmobile

This Repository is NOT a supported MongoDB product
Swift
2
star
53

django-mongodb

MongoDB Backend for Django
Python
2
star
54

mongo-ebpf-tools

Python
2
star
55

helium-ti-poc

Repo for UI prototype of TI-Helium POC
TypeScript
1
star
56

icecream

Icecream package repository for developer workstations
C++
1
star
57

python-service-tools

A repository to share common tools for working with python services.
Python
1
star
58

driver-performance-test-data

Datasets for MongoDB's internal driver benchmarking experiments - This Repository is NOT a supported MongoDB product
1
star
59

mongo-netopo

[Archive] Intern project - Network Topology Mapper for MongoDB - This Repository is NOT a supported MongoDB product
C++
1
star
60

mongodb-aem-course

Adobe Experience Manager and MongoDB Online Education Course - This Repository is NOT a supported MongoDB product
Shell
1
star
61

mpc-cli

Go
1
star
62

mongodb-cloudloads

Cloud-native MongoDB sample apps and workloads.
1
star
63

mgobson

Housing a wrapper around mgo's BSON library - This Repository is NOT a supported MongoDB product
Go
1
star
64

mongo-recsys

[Archive] Intern project - Recommendation system using MongoDB - This Repository is NOT a supported MongoDB product
Java
1
star
65

mongoid-fle-sample-app

Ruby on Rails Application that uses Mongoid/MongoDB to demonstrate Client-Side Field Level Encryption
Ruby
1
star
66

mdbrtools

Experimental tools for schema analysis and query workload generation used by MongoDB Research
Python
1
star
67

slogger

Go logging library - This Repository is NOT a supported MongoDB product
Go
1
star