Skip to content

Commit

Permalink
Merge branch 'master' into ghmeier/uprade-liniting
Browse files Browse the repository at this point in the history
  • Loading branch information
Garret Meier committed Oct 15, 2019
2 parents 9cf9249 + 07705c5 commit 3c6d991
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
21 changes: 19 additions & 2 deletions README.md
Expand Up @@ -4,8 +4,8 @@ A [node.js](http://nodejs.org) module for mongodb built with async/await in mind

Mongoist driver is heavily inspired by mongojs.

[![Build Status](https://travis-ci.org/saintedlama/mongoist.svg?branch=master)](https://travis-ci.org/saintedlama/mongoist)
[![Coverage Status](https://coveralls.io/repos/github/saintedlama/mongoist/badge.svg?branch=master)](https://coveralls.io/github/saintedlama/mongoist?branch=master)
[![Build Status](https://travis-ci.org/mongoist/mongoist.svg?branch=master)](https://travis-ci.org/mongoist/mongoist)
[![Coverage Status](https://coveralls.io/repos/github/mongoist/mongoist/badge.svg?branch=master)](https://coveralls.io/github/mongoist/mongoist?branch=master)

## Motivation

Expand Down Expand Up @@ -181,6 +181,14 @@ const result = await db.things.runCommand('count');
console.log(result);
```

Similarly, you can use `db.adminCommand()` to run a command on the `admin` database of the
MongoDB cluster or instance you're connected to:

```js
const result = await db.adminCommand({currentOp: 1});
console.log(result);
```

### Replication Sets

Mongoist can connect to a mongo replication set by providing a connection string with multiple hosts
Expand Down Expand Up @@ -318,6 +326,10 @@ See https://docs.mongodb.com/manual/reference/method/db.collection.remove/

See https://docs.mongodb.com/manual/reference/method/db.collection.remove/

#### `db.collection.replaceOne(filter, replacement, [options])`

See https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/

#### `db.collection.runCommand(command)`

See https://docs.mongodb.com/manual/reference/method/db.collection.runCommand/
Expand Down Expand Up @@ -451,6 +463,11 @@ See https://docs.mongodb.com/manual/reference/method/db.getLastErrorObj/

See https://docs.mongodb.com/manual/reference/method/db.runCommand/


#### `db.adminCommand(command)`

See https://mongodb.github.io/node-mongodb-native/3.3/api/Db.html#executeDbAdminCommand

#### `db.stats()`

See https://docs.mongodb.com/manual/reference/method/db.stats/
Expand Down
8 changes: 8 additions & 0 deletions lib/collection.js
Expand Up @@ -108,6 +108,14 @@ module.exports = class Collection {
.then((result) => result.result);
}

replaceOne(filter, replacement, opts) {
opts = opts || {};

return this
.connect()
.then(connection => connection.replaceOne(filter, replacement, Object.assign({}, writeOpts, opts)));
}

save(doc, opts) {
opts = opts || {};

Expand Down
12 changes: 11 additions & 1 deletion lib/database.js
Expand Up @@ -4,6 +4,10 @@ const urlParser = require('mongodb/lib/url_parser.js');
const Collection = require('./collection');
const debug = require('debug')('mongoist');

function normalizeCommandOpts(opts) {
return typeof opts === 'string' ? { [opts]: 1 } : opts;
}

module.exports = class Database extends EventEmitter {
constructor(connectionString, options) {
super();
Expand Down Expand Up @@ -48,6 +52,12 @@ module.exports = class Database extends EventEmitter {
return this.listCollections().then((collections) => collections.map((collection) => collection.name));
}

adminCommand(opts) {
return this
.connect()
.then(connection => connection.executeDbAdminCommand(normalizeCommandOpts(opts)));
}

runCommand(opts) {
if (typeof opts === 'string') {
opts = {
Expand All @@ -56,7 +66,7 @@ module.exports = class Database extends EventEmitter {

return this
.connect()
.then((connection) => connection.command(opts));
.then((connection) => connection.command(normalizeCommandOpts(opts)));
}

stats(scale) {
Expand Down
22 changes: 20 additions & 2 deletions test/collection.js
Expand Up @@ -156,8 +156,26 @@ describe('collection', function() {
const idleDocsCount = await db.a.count({ type: 'water'});
expect(idleDocsCount).equal(0);
});

it('should save a document', async () => {

it('should replace a document', async() => {
const bulbasaurLevel9 = {
name: 'Bulbasaur',
type: 'grass',
level: 9
};
const bulbasaurLevel10 = Object.assign({}, bulbasaurLevel9, { level: 10 });
const upsertedReplace = await db.a.replaceOne(bulbasaurLevel9, bulbasaurLevel10, { upsert: true });
expect(upsertedReplace.matchedCount).to.equal(0);
expect(upsertedReplace.modifiedCount).to.equal(0);
expect(upsertedReplace.upsertedId).to.exist;

const replaced = await db.a.replaceOne(bulbasaurLevel10, bulbasaurLevel10);
expect(replaced.matchedCount).equal(1);
expect(replaced.modifiedCount).equal(1);
expect(replaced.upsertedId).to.not.exist;
});

it('should save a document', async() => {
const doc = await db.a.save({name: 'Charizard', type: 'fire'});

expect(doc._id).to.exist;
Expand Down
5 changes: 5 additions & 0 deletions test/database.js
Expand Up @@ -296,4 +296,9 @@ describe('database', function() {
await db.close();
await db2.close();
});

it('should execute admin commands', async() => {
const dbStats = await db.adminCommand({ dbStats: 1 });
expect(dbStats.db).to.equal('admin');
});
});

0 comments on commit 3c6d991

Please sign in to comment.