Skip to content

Instantly share code, notes, and snippets.

let cursor = match coll.find(doc! { age: { $gte: 18 } } , None) {
Ok(cursor) => cursor,
Err(error) => println!("The following error occured: {}", error)
};
// Generate a list of InsertOne write models.
let models = (1..5).map(|i| WriteModel::InsertOne { document: doc! {
"_id" => (i),
"x" => (i * 11)
}}).collect();
let new_coll = db.collection("example");
// Execute InsertOne operations, ordered.
new_coll.bulk_write(models, true);
let coll = db.collection("comedies");
let film_a = doc!{ "title" => "Ferris Bueller’s Day Off" };
let film_b = doc!{ "title" => "Airplane!" };
// Insert multiple documents with default options.
coll.insert_many(vec![film_a, film_b], None)
.ok().expect("Failed to insert documents.");
// Update a single document.
db.auth("root", "very_secure_password")
.ok().expect("Failed to authorize user 'root'.");
// List collections without a filter.
let collections = db.list_collections(None)
.ok().expect(“Are you sure you’ve been authorized?”);
let db = client.db("movies");
let options = CreateCollectionOptions::new();
options.capped = true;
options.size = Some(100000);
db.create_collection("comedies", Some(options))
.ok().expect("Failed to create 'comedies' collection!");
let client = Client::with_uri("mongodb://localhost:27017,localhost:27018")
.ok().expect("Failed to initialize client.");
let client = Client::connect("localhost", 27017)
.ok().expect("Failed to initialize client.");
#[macro_use(bson, doc)]
extern crate bson;
extern crate mongodb;
[dependencies]
bson = "0.1.3"
mongodb = "0.1.0"
local MongoClient = require("luaMongo.MongoClient")
local client = MongoClient.new("mongodb://localhost:27017/")
local exampleDatabase = client:getDatabase("exampleDB")
local exampleCollection = exampleDatabase:getCollection("exampleCollection")
-- Create document to insert.
local document_to_insert = {foo = "bar"}
-- Insert document into database
exampleCollection:insert_one(document_to_insert)