Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,60 @@ Db.prototype.createCollection = function(name, options, callback) {

define.classMethod('createCollection', {callback: true, promise:true});

/**
* Create view on MongoDB 3.4 or higher
*
* @method
* @param {string} name Name of view
* @param {string} viewOn The name of the source collection or view from which to create the view. The name is not the full namespace of the collection or view; i.e. does not include the database name and implies the same database as the view to create on MongoDB 3.4 or higher.
* @param {array} pipeline An array that consists of the aggregation pipeline stage. create creates the view by applying the specified pipeline to the viewOn collection or view on MongoDB 3.4 or higher.
* @param {object} [options=null] Optional settings.
* @param {Db~resultCallback} [callback] The results callback
* @return {Promise} returns Promise if no callback passed
*/
Db.prototype.createView = function(name, viewOn, pipeline, options, callback) {
var self = this;
if(typeof pipeline == 'function') callback = pipeline, pipeline = [];
if(typeof options == 'function') callback = options, options = {};
options = options || {};

// Create view command
var cmd = {'create': name};

if (viewOn == undefined) {
throw Error("Must specify a backing view or collection");
}

if (pipeline != undefined) {
if (!Array.isArray(pipeline)) {
pipeline = [pipeline];
}
}

cmd.pipeline = pipeline;
cmd.viewOn = viewOn;

// Check if the callback is in fact a function
if(typeof callback == 'function') return this.command(cmd, options, function(err, result) {
// Did the user destroy the topology
if(err) return handleCallback(callback, err, null);
handleCallback(callback, null, result.ok ? true : false);
});

// Execute the command
return new this.s.promiseLibrary(function(resolve, reject) {
// Execute command
self.command(cmd, options, function(err, result) {
// Did the user destroy the topology
if(err) return reject(err);
if(result.ok) return resolve(true);
resolve(false);
});
});
}

define.classMethod('createView', {callback: true, promise:true});

/**
* Get all the db statistics.
*
Expand Down
25 changes: 25 additions & 0 deletions test/functional/db_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,28 @@ exports['should correctly execute close function in order'] = {
});
}
}

/**
* @ignore
*/
exports.shouldCorrectlyGetErrorCreatingExistingView = {
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },

// The actual test we wish to run
test: function(configuration, test) {
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1, auto_reconnect:false});

// Establish connection to db
db.open(function(err, db) {
var _db = db.db("nonexistingdb");
// Let's create the view
_db.createView('test_create_view', 'test_resave_dbref', function(err, result) {
test.equal(null, err);
test.equal(true, result);

db.close();
test.done();
});
});
}
}