From 33f7f18a23a27ea5c8524e7514723bb0720bcb88 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sun, 20 Oct 2013 15:41:40 +0200 Subject: [PATCH] Updated readme --- README.md | 14 ++++++++++++++ test/db.test.js | 3 +-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2d85316..ce3f1fe 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,20 @@ db.insert(document, function (err, newDoc) { // Callback is optional }); ``` +You can also bulk-insert an array of documents. This operation is atomic, meaning that if one insert fails due to a unique constraint being violated, all changes are rolled back. +```javascript +db.insert([{ a: 5 }, { a: 42 }], function (err, newDocs) { + // Two documents were inserted in the database + // newDocs is an array with these documents, augmented with their _id +}); + +// If there is a unique constraint on field 'a', this will fail +db.insert([{ a: 5 }, { a: 42 }, { a: 5 }], function (err) { + // err is a 'uniqueViolated' error + // The database was not modified +}); +``` + ### Finding documents Use `find` to look for multiple documents matching you query, or `findOne` to look for one specific document. You can select documents based on field equality or use comparison operators (`$lt`, `$lte`, `$gt`, `$gte`, `$in`, `$nin`, `$ne`). You can also use logical operators `$or`, `$and` and `$not`. See below for the syntax. diff --git a/test/db.test.js b/test/db.test.js index 8f40695..4bc6809 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -210,8 +210,7 @@ describe('Database', function () { d.ensureIndex({ fieldName: 'a', unique: true }); d.insert(docs, function (err) { - assert.isDefined(err); - assert.isNotNull(err); + err.errorType.should.equal('uniqueViolated'); d.find({}, function (err, docs) { docs.length.should.equal(0);