Skip to content

Commit 7931bd1

Browse files
committed
NODE-473 NODE-545 improved API documentation
1 parent 3cb2435 commit 7931bd1

File tree

4 files changed

+111
-15
lines changed

4 files changed

+111
-15
lines changed

conf.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib/bulk/ordered.js",
2222
"lib/bulk/unordered.js",
2323
"lib/gridfs/grid_store.js",
24+
"node_modules/mongodb-core/lib/error.js",
2425
"node_modules/mongodb-core/lib/connection/logger.js",
2526
"node_modules/bson/lib/bson/binary.js",
2627
"node_modules/bson/lib/bson/code.js",

lib/collection.js

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ Collection.prototype.find = function() {
350350
* @param {boolean} [options.j=false] Specify a journal write concern.
351351
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
352352
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
353-
* @param {Collection~writeOpCallback} [callback] The command result callback
353+
* @param {Collection~insertWriteOpCallback} [callback] The command result callback
354354
* @return {Promise} returns Promise if no callback passed
355355
*/
356356
Collection.prototype.insertOne = function(doc, options, callback) {
@@ -413,7 +413,7 @@ var mapInserManyResults = function(docs, r) {
413413
* @param {boolean} [options.j=false] Specify a journal write concern.
414414
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
415415
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
416-
* @param {Collection~writeOpCallback} [callback] The command result callback
416+
* @param {Collection~insertWriteOpCallback} [callback] The command result callback
417417
* @return {Promise} returns Promise if no callback passed
418418
*/
419419
Collection.prototype.insertMany = function(docs, options, callback) {
@@ -641,6 +641,24 @@ var insertDocuments = function(self, docs, options, callback) {
641641
* @param {Collection~WriteOpResult} result The result object if the command was executed successfully.
642642
*/
643643

644+
/**
645+
* @typedef {Object} Collection~insertWriteOpResult
646+
* @property {Number} insertedCount The total amount of documents inserted.
647+
* @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
648+
* @property {ObjectId[]} insertedIds All the generated _id's for the inserted documents.
649+
* @property {object} connection The connection object used for the operation.
650+
* @property {object} result The raw command result object returned from MongoDB (content might vary by server version).
651+
* @property {Number} result.ok Is 1 if the command executed correctly.
652+
* @property {Number} result.n The total count of documents inserted.
653+
*/
654+
655+
/**
656+
* The callback format for inserts
657+
* @callback Collection~insertWriteOpCallback
658+
* @param {MongoError} error An error instance representing the error during the execution.
659+
* @param {Collection~insertWriteOpResult} result The result object if the command was executed successfully.
660+
*/
661+
644662
/**
645663
* Inserts a single document or a an array of documents into MongoDB.
646664
* @method
@@ -651,7 +669,7 @@ var insertDocuments = function(self, docs, options, callback) {
651669
* @param {boolean} [options.j=false] Specify a journal write concern.
652670
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
653671
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
654-
* @param {Collection~writeOpCallback} [callback] The command result callback
672+
* @param {Collection~insertWriteOpCallback} [callback] The command result callback
655673
* @return {Promise} returns Promise if no callback passed
656674
* @deprecated Use insertOne, insertMany or bulkWrite
657675
*/
@@ -668,6 +686,27 @@ Collection.prototype.insert = function(docs, options, callback) {
668686
return this.insertMany(docs, options, callback);
669687
}
670688

689+
/**
690+
* @typedef {Object} Collection~updateWriteOpResult
691+
* @property {Object} result The raw result returned from MongoDB, field will vary depending on server version.
692+
* @property {Number} result.ok Is 1 if the command executed correctly.
693+
* @property {Number} result.n The total count of documents scanned.
694+
* @property {Number} result.nModified The total count of documents modified.
695+
* @property {Object} connection The connection object used for the operation.
696+
* @property {Number} matchedCount The number of documents that matched the filter.
697+
* @property {Number} modifiedCount The number of documents that were modified.
698+
* @property {Number} upsertedCount The number of documents upserted.
699+
* @property {Object} upsertedId The upserted id.
700+
* @property {ObjectId} upsertedId._id The upserted _id returned from the server.
701+
*/
702+
703+
/**
704+
* The callback format for inserts
705+
* @callback Collection~updateWriteOpCallback
706+
* @param {MongoError} error An error instance representing the error during the execution.
707+
* @param {Collection~updateWriteOpResult} result The result object if the command was executed successfully.
708+
*/
709+
671710
/**
672711
* Update a single document on MongoDB
673712
* @method
@@ -678,7 +717,7 @@ Collection.prototype.insert = function(docs, options, callback) {
678717
* @param {(number|string)} [options.w=null] The write concern.
679718
* @param {number} [options.wtimeout=null] The write concern timeout.
680719
* @param {boolean} [options.j=false] Specify a journal write concern.
681-
* @param {Collection~writeOpCallback} [callback] The command result callback
720+
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
682721
* @return {Promise} returns Promise if no callback passed
683722
*/
684723
Collection.prototype.updateOne = function(filter, update, options, callback) {
@@ -724,7 +763,7 @@ var updateOne = function(self, filter, update, options, callback) {
724763
* @param {(number|string)} [options.w=null] The write concern.
725764
* @param {number} [options.wtimeout=null] The write concern timeout.
726765
* @param {boolean} [options.j=false] Specify a journal write concern.
727-
* @param {Collection~writeOpCallback} [callback] The command result callback
766+
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
728767
* @return {Promise} returns Promise if no callback passed
729768
*/
730769
Collection.prototype.replaceOne = function(filter, update, options, callback) {
@@ -771,7 +810,7 @@ var replaceOne = function(self, filter, update, options, callback) {
771810
* @param {(number|string)} [options.w=null] The write concern.
772811
* @param {number} [options.wtimeout=null] The write concern timeout.
773812
* @param {boolean} [options.j=false] Specify a journal write concern.
774-
* @param {Collection~writeOpCallback} [callback] The command result callback
813+
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
775814
* @return {Promise} returns Promise if no callback passed
776815
*/
777816
Collection.prototype.updateMany = function(filter, update, options, callback) {
@@ -871,6 +910,22 @@ Collection.prototype.update = function(selector, document, options, callback) {
871910
});
872911
}
873912

913+
/**
914+
* @typedef {Object} Collection~deleteWriteOpResult
915+
* @property {Object} result The raw result returned from MongoDB, field will vary depending on server version.
916+
* @property {Number} result.ok Is 1 if the command executed correctly.
917+
* @property {Number} result.n The total count of documents deleted.
918+
* @property {Object} connection The connection object used for the operation.
919+
* @property {Number} deletedCount The number of documents deleted.
920+
*/
921+
922+
/**
923+
* The callback format for inserts
924+
* @callback Collection~deleteWriteOpCallback
925+
* @param {MongoError} error An error instance representing the error during the execution.
926+
* @param {Collection~deleteWriteOpResult} result The result object if the command was executed successfully.
927+
*/
928+
874929
/**
875930
* Delete a document on MongoDB
876931
* @method
@@ -879,7 +934,7 @@ Collection.prototype.update = function(selector, document, options, callback) {
879934
* @param {(number|string)} [options.w=null] The write concern.
880935
* @param {number} [options.wtimeout=null] The write concern timeout.
881936
* @param {boolean} [options.j=false] Specify a journal write concern.
882-
* @param {Collection~writeOpCallback} [callback] The command result callback
937+
* @param {Collection~deleteWriteOpCallback} [callback] The command result callback
883938
* @return {Promise} returns Promise if no callback passed
884939
*/
885940
Collection.prototype.deleteOne = function(filter, options, callback) {
@@ -920,7 +975,7 @@ Collection.prototype.removeOne = Collection.prototype.deleteOne;
920975
* @param {(number|string)} [options.w=null] The write concern.
921976
* @param {number} [options.wtimeout=null] The write concern timeout.
922977
* @param {boolean} [options.j=false] Specify a journal write concern.
923-
* @param {Collection~writeOpCallback} [callback] The command result callback
978+
* @param {Collection~deleteWriteOpCallback} [callback] The command result callback
924979
* @return {Promise} returns Promise if no callback passed
925980
*/
926981
Collection.prototype.deleteMany = function(filter, options, callback) {
@@ -1025,6 +1080,7 @@ Collection.prototype.remove = function(selector, options, callback) {
10251080
* @param {boolean} [options.j=false] Specify a journal write concern.
10261081
* @param {Collection~writeOpCallback} [callback] The command result callback
10271082
* @return {Promise} returns Promise if no callback passed
1083+
* @deprecated use insertOne, insertMany, updateOne or updateMany
10281084
*/
10291085
Collection.prototype.save = function(doc, options, callback) {
10301086
var self = this;
@@ -1808,6 +1864,20 @@ var stats = function(self, options, callback) {
18081864
self.s.db.command(commandObject, options, callback);
18091865
}
18101866

1867+
/**
1868+
* @typedef {Object} Collection~findAndModifyWriteOpResult
1869+
* @property {object} value Document returned from findAndModify command.
1870+
* @property {object} lastErrorObject The raw lastErrorObject returned from the command.
1871+
* @property {Number} ok Is 1 if the command executed correctly.
1872+
*/
1873+
1874+
/**
1875+
* The callback format for inserts
1876+
* @callback Collection~findAndModifyCallback
1877+
* @param {MongoError} error An error instance representing the error during the execution.
1878+
* @param {Collection~findAndModifyWriteOpResult} result The result object if the command was executed successfully.
1879+
*/
1880+
18111881
/**
18121882
* Find a document and delete it in one atomic operation, requires a write lock for the duration of the operation.
18131883
*
@@ -1817,7 +1887,7 @@ var stats = function(self, options, callback) {
18171887
* @param {object} [options.projection=null] Limits the fields to return for all matching documents.
18181888
* @param {object} [options.sort=null] Determines which document the operation modifies if the query selects multiple documents.
18191889
* @param {number} [options.maxTimeMS=null] The maximum amount of time to allow the query to run.
1820-
* @param {Collection~resultCallback} [callback] The collection result callback
1890+
* @param {Collection~findAndModifyCallback} [callback] The collection result callback
18211891
* @return {Promise} returns Promise if no callback passed
18221892
*/
18231893
Collection.prototype.findOneAndDelete = function(filter, options, callback) {
@@ -1864,7 +1934,7 @@ var findOneAndDelete = function(self, filter, options, callback) {
18641934
* @param {number} [options.maxTimeMS=null] The maximum amount of time to allow the query to run.
18651935
* @param {boolean} [options.upsert=false] Upsert the document if it does not exist.
18661936
* @param {boolean} [options.returnOriginal=true] When false, returns the updated document rather than the original. The default is true.
1867-
* @param {Collection~resultCallback} [callback] The collection result callback
1937+
* @param {Collection~findAndModifyCallback} [callback] The collection result callback
18681938
* @return {Promise} returns Promise if no callback passed
18691939
*/
18701940
Collection.prototype.findOneAndReplace = function(filter, replacement, options, callback) {
@@ -1913,7 +1983,7 @@ var findOneAndReplace = function(self, filter, replacement, options, callback) {
19131983
* @param {number} [options.maxTimeMS=null] The maximum amount of time to allow the query to run.
19141984
* @param {boolean} [options.upsert=false] Upsert the document if it does not exist.
19151985
* @param {boolean} [options.returnOriginal=true] When false, returns the updated document rather than the original. The default is true.
1916-
* @param {Collection~resultCallback} [callback] The collection result callback
1986+
* @param {Collection~findAndModifyCallback} [callback] The collection result callback
19171987
* @return {Promise} returns Promise if no callback passed
19181988
*/
19191989
Collection.prototype.findOneAndUpdate = function(filter, update, options, callback) {

lib/db.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ var evaluate = function(self, code, parameters, options, callback) {
640640
* @param {boolean} [options.nolock=false] Tell MongoDB not to block on the evaulation of the javascript.
641641
* @param {Db~resultCallback} [callback] The results callback
642642
* @return {Promise} returns Promise if no callback passed
643+
* @deprecated MongoDB 3.2 and higher no longer support eval.
643644
*/
644645
Db.prototype.eval = function(code, parameters, options, callback) {
645646
var self = this;
@@ -1585,47 +1586,65 @@ var createListener = function(self, e, object) {
15851586
/**
15861587
* Db close event
15871588
*
1589+
* Emitted after a socket closed against a single server or mongos proxy.
1590+
*
15881591
* @event Db#close
1589-
* @type {object}
1592+
* @type {MongoError}
15901593
*/
15911594

15921595
/**
15931596
* Db authenticated event
15941597
*
1598+
* Emitted after all server members in the topology (single server, replicaset or mongos) have successfully authenticated.
1599+
*
15951600
* @event Db#authenticated
15961601
* @type {object}
15971602
*/
15981603

15991604
/**
16001605
* Db reconnect event
16011606
*
1607+
* * Server: Emitted when the driver has reconnected and re-authenticated.
1608+
* * ReplicaSet: N/A
1609+
* * Mongos: Emitted when the driver reconnects and re-authenticates successfully against a Mongos.
1610+
*
16021611
* @event Db#reconnect
16031612
* @type {object}
16041613
*/
16051614

16061615
/**
16071616
* Db error event
16081617
*
1618+
* Emitted after an error occurred against a single server or mongos proxy.
1619+
*
16091620
* @event Db#error
16101621
* @type {MongoError}
16111622
*/
16121623

16131624
/**
16141625
* Db timeout event
16151626
*
1627+
* Emitted after a socket timeout occurred against a single server or mongos proxy.
1628+
*
16161629
* @event Db#timeout
1617-
* @type {object}
1630+
* @type {MongoError}
16181631
*/
16191632

16201633
/**
16211634
* Db parseError event
16221635
*
1636+
* The parseError event is emitted if the driver detects illegal or corrupt BSON being received from the server.
1637+
*
16231638
* @event Db#parseError
1624-
* @type {object}
1639+
* @type {MongoError}
16251640
*/
16261641

16271642
/**
1628-
* Db fullsetup event, emitted when all servers in the topology have been connected to.
1643+
* Db fullsetup event, emitted when all servers in the topology have been connected to at start up time.
1644+
*
1645+
* * Server: Emitted when the driver has connected to the single server and has authenticated.
1646+
* * ReplSet: Emitted after the driver has attempted to connect to all replicaset members.
1647+
* * Mongos: Emitted after the driver has attempted to connect to all mongos proxies.
16291648
*
16301649
* @event Db#fullsetup
16311650
* @type {Db}

test/functional/crud_api_tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ exports['should correctly execute findAndModify methods using crud api'] = {
708708
db.collection('t5_1').findOneAndDelete({a:1}
709709
, { projection: {b:1}, sort: {a:1} }
710710
, function(err, r) {
711+
console.log("------------------------------------------------ findOneAndDelete")
712+
console.dir(r)
711713
test.equal(null, err);
712714
test.equal(1, r.lastErrorObject.n);
713715
test.equal(1, r.value.b);
@@ -734,6 +736,8 @@ exports['should correctly execute findAndModify methods using crud api'] = {
734736
, upsert: true
735737
}
736738
, function(err, r) {
739+
console.log("------------------------------------------------ findOneAndReplace")
740+
console.dir(r)
737741
test.equal(null, err);
738742
test.equal(1, r.lastErrorObject.n);
739743
test.equal(1, r.value.b);
@@ -761,6 +765,8 @@ exports['should correctly execute findAndModify methods using crud api'] = {
761765
, upsert: true
762766
}
763767
, function(err, r) {
768+
console.log("------------------------------------------------ findOneAndUpdate")
769+
console.dir(r)
764770
test.equal(null, err);
765771
test.equal(1, r.lastErrorObject.n);
766772
test.equal(1, r.value.b);

0 commit comments

Comments
 (0)