collection.js 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. "use strict";
  2. var checkCollectionName = require('./utils').checkCollectionName
  3. , ObjectID = require('mongodb-core').BSON.ObjectID
  4. , Long = require('mongodb-core').BSON.Long
  5. , Code = require('mongodb-core').BSON.Code
  6. , f = require('util').format
  7. , AggregationCursor = require('./aggregation_cursor')
  8. , MongoError = require('mongodb-core').MongoError
  9. , shallowClone = require('./utils').shallowClone
  10. , isObject = require('./utils').isObject
  11. , toError = require('./utils').toError
  12. , normalizeHintField = require('./utils').normalizeHintField
  13. , handleCallback = require('./utils').handleCallback
  14. , decorateCommand = require('./utils').decorateCommand
  15. , formattedOrderClause = require('./utils').formattedOrderClause
  16. , ReadPreference = require('./read_preference')
  17. , CoreReadPreference = require('mongodb-core').ReadPreference
  18. , CommandCursor = require('./command_cursor')
  19. , Cursor = require('./cursor')
  20. , unordered = require('./bulk/unordered')
  21. , ordered = require('./bulk/ordered');
  22. /**
  23. * @fileOverview The **Collection** class is an internal class that embodies a MongoDB collection
  24. * allowing for insert/update/remove/find and other command operation on that MongoDB collection.
  25. *
  26. * **COLLECTION Cannot directly be instantiated**
  27. * @example
  28. * var MongoClient = require('mongodb').MongoClient,
  29. * test = require('assert');
  30. * // Connection url
  31. * var url = 'mongodb://localhost:27017/test';
  32. * // Connect using MongoClient
  33. * MongoClient.connect(url, function(err, db) {
  34. * // Create a collection we want to drop later
  35. * var col = db.collection('createIndexExample1');
  36. * // Show that duplicate records got dropped
  37. * col.find({}).toArray(function(err, items) {
  38. * test.equal(null, err);
  39. * test.equal(4, items.length);
  40. * db.close();
  41. * });
  42. * });
  43. */
  44. /**
  45. * Create a new Collection instance (INTERNAL TYPE, do not instantiate directly)
  46. * @class
  47. * @property {string} collectionName Get the collection name.
  48. * @property {string} namespace Get the full collection namespace.
  49. * @property {object} writeConcern The current write concern values.
  50. * @property {object} hint Get current index hint for collection.
  51. * @return {Collection} a Collection instance.
  52. */
  53. var Collection = function(db, topology, dbName, name, pkFactory, options) {
  54. checkCollectionName(name);
  55. var self = this;
  56. // Unpack variables
  57. var internalHint = null;
  58. var opts = options != null && ('object' === typeof options) ? options : {};
  59. var slaveOk = options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk;
  60. var serializeFunctions = options == null || options.serializeFunctions == null ? db.serializeFunctions : options.serializeFunctions;
  61. var raw = options == null || options.raw == null ? db.raw : options.raw;
  62. var readPreference = null;
  63. var collectionHint = null;
  64. var namespace = f("%s.%s", dbName, name);
  65. // Assign the right collection level readPreference
  66. if(options && options.readPreference) {
  67. readPreference = options.readPreference;
  68. } else if(db.options.readPreference) {
  69. readPreference = db.options.readPreference;
  70. }
  71. // Set custom primary key factory if provided
  72. pkFactory = pkFactory == null
  73. ? ObjectID
  74. : pkFactory;
  75. // Internal state
  76. this.s = {
  77. // Set custom primary key factory if provided
  78. pkFactory: pkFactory
  79. // Db
  80. , db: db
  81. // Topology
  82. , topology: topology
  83. // dbName
  84. , dbName: dbName
  85. // Options
  86. , options: options
  87. // Namespace
  88. , namespace: namespace
  89. // Read preference
  90. , readPreference: readPreference
  91. // Raw
  92. , raw: raw
  93. // SlaveOK
  94. , slaveOk: slaveOk
  95. // Serialize functions
  96. , serializeFunctions: serializeFunctions
  97. // internalHint
  98. , internalHint: internalHint
  99. // collectionHint
  100. , collectionHint: collectionHint
  101. // Name
  102. , name: name
  103. }
  104. }
  105. Object.defineProperty(Collection.prototype, 'collectionName', {
  106. enumerable: true, get: function() { return this.s.name; }
  107. });
  108. Object.defineProperty(Collection.prototype, 'namespace', {
  109. enumerable: true, get: function() { return this.s.namespace; }
  110. });
  111. Object.defineProperty(Collection.prototype, 'writeConcern', {
  112. enumerable:true,
  113. get: function() {
  114. var ops = {};
  115. if(this.s.options.w != null) ops.w = this.s.options.w;
  116. if(this.s.options.j != null) ops.j = this.s.options.j;
  117. if(this.s.options.fsync != null) ops.fsync = this.s.options.fsync;
  118. if(this.s.options.wtimeout != null) ops.wtimeout = this.s.options.wtimeout;
  119. return ops;
  120. }
  121. });
  122. /**
  123. * @ignore
  124. */
  125. Object.defineProperty(Collection.prototype, "hint", {
  126. enumerable: true
  127. , get: function () { return this.s.collectionHint; }
  128. , set: function (v) { this.s.collectionHint = normalizeHintField(v); }
  129. });
  130. /**
  131. * Creates a cursor for a query that can be used to iterate over results from MongoDB
  132. * @method
  133. * @param {object} query The cursor query object.
  134. * @throws {MongoError}
  135. * @return {Cursor}
  136. */
  137. Collection.prototype.find = function() {
  138. var options
  139. , args = Array.prototype.slice.call(arguments, 0)
  140. , has_callback = typeof args[args.length - 1] === 'function'
  141. , has_weird_callback = typeof args[0] === 'function'
  142. , callback = has_callback ? args.pop() : (has_weird_callback ? args.shift() : null)
  143. , len = args.length
  144. , selector = len >= 1 ? args[0] : {}
  145. , fields = len >= 2 ? args[1] : undefined;
  146. if(len === 1 && has_weird_callback) {
  147. // backwards compat for callback?, options case
  148. selector = {};
  149. options = args[0];
  150. }
  151. if(len === 2 && !Array.isArray(fields)) {
  152. var fieldKeys = Object.keys(fields);
  153. var is_option = false;
  154. for(var i = 0; i < fieldKeys.length; i++) {
  155. if(testForFields[fieldKeys[i]] != null) {
  156. is_option = true;
  157. break;
  158. }
  159. }
  160. if(is_option) {
  161. options = fields;
  162. fields = undefined;
  163. } else {
  164. options = {};
  165. }
  166. } else if(len === 2 && Array.isArray(fields) && !Array.isArray(fields[0])) {
  167. var newFields = {};
  168. // Rewrite the array
  169. for(var i = 0; i < fields.length; i++) {
  170. newFields[fields[i]] = 1;
  171. }
  172. // Set the fields
  173. fields = newFields;
  174. }
  175. if(3 === len) {
  176. options = args[2];
  177. }
  178. // Ensure selector is not null
  179. selector = selector == null ? {} : selector;
  180. // Validate correctness off the selector
  181. var object = selector;
  182. if(Buffer.isBuffer(object)) {
  183. var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
  184. if(object_size != object.length) {
  185. var error = new Error("query selector raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
  186. error.name = 'MongoError';
  187. throw error;
  188. }
  189. }
  190. // Validate correctness of the field selector
  191. var object = fields;
  192. if(Buffer.isBuffer(object)) {
  193. var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
  194. if(object_size != object.length) {
  195. var error = new Error("query fields raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
  196. error.name = 'MongoError';
  197. throw error;
  198. }
  199. }
  200. // Check special case where we are using an objectId
  201. if(selector instanceof ObjectID || (selector != null && selector._bsontype == 'ObjectID')) {
  202. selector = {_id:selector};
  203. }
  204. // If it's a serialized fields field we need to just let it through
  205. // user be warned it better be good
  206. if(options && options.fields && !(Buffer.isBuffer(options.fields))) {
  207. fields = {};
  208. if(Array.isArray(options.fields)) {
  209. if(!options.fields.length) {
  210. fields['_id'] = 1;
  211. } else {
  212. for (var i = 0, l = options.fields.length; i < l; i++) {
  213. fields[options.fields[i]] = 1;
  214. }
  215. }
  216. } else {
  217. fields = options.fields;
  218. }
  219. }
  220. if (!options) options = {};
  221. var newOptions = {};
  222. // Make a shallow copy of options
  223. for (var key in options) {
  224. newOptions[key] = options[key];
  225. }
  226. // Unpack options
  227. newOptions.skip = len > 3 ? args[2] : options.skip ? options.skip : 0;
  228. newOptions.limit = len > 3 ? args[3] : options.limit ? options.limit : 0;
  229. newOptions.raw = options.raw != null && typeof options.raw === 'boolean' ? options.raw : this.s.raw;
  230. newOptions.hint = options.hint != null ? normalizeHintField(options.hint) : this.s.collectionHint;
  231. newOptions.timeout = len == 5 ? args[4] : typeof options.timeout === 'undefined' ? undefined : options.timeout;
  232. // // If we have overridden slaveOk otherwise use the default db setting
  233. newOptions.slaveOk = options.slaveOk != null ? options.slaveOk : this.s.db.slaveOk;
  234. // Add read preference if needed
  235. newOptions = getReadPreference(this, newOptions, this.s.db, this);
  236. // Set slave ok to true if read preference different from primary
  237. if(newOptions.readPreference != null
  238. && (newOptions.readPreference != 'primary' || newOptions.readPreference.mode != 'primary')) {
  239. newOptions.slaveOk = true;
  240. }
  241. // Ensure the query is an object
  242. if(selector != null && typeof selector != 'object') {
  243. throw new MongoError("query selector must be an object");
  244. }
  245. // Build the find command
  246. var findCommand = {
  247. find: this.s.namespace
  248. , limit: newOptions.limit
  249. , skip: newOptions.skip
  250. , query: selector
  251. }
  252. // Ensure we use the right await data option
  253. if(typeof newOptions.awaitdata == 'boolean') {
  254. newOptions.awaitData = newOptions.awaitdata
  255. };
  256. // Translate to new command option noCursorTimeout
  257. if(typeof newOptions.timeout == 'boolean') newOptions.noCursorTimeout = newOptions.timeout;
  258. // Merge in options to command
  259. for(var name in newOptions) {
  260. if(newOptions[name] != null) findCommand[name] = newOptions[name];
  261. }
  262. // Format the fields
  263. var formatFields = function(fields) {
  264. var object = {};
  265. if(Array.isArray(fields)) {
  266. for(var i = 0; i < fields.length; i++) {
  267. if(Array.isArray(fields[i])) {
  268. object[fields[i][0]] = fields[i][1];
  269. } else {
  270. object[fields[i][0]] = 1;
  271. }
  272. }
  273. } else {
  274. object = fields;
  275. }
  276. return object;
  277. }
  278. // Special treatment for the fields selector
  279. if(fields) findCommand.fields = formatFields(fields);
  280. // Add db object to the new options
  281. newOptions.db = this.s.db;
  282. // Set raw if available at collection level
  283. if(newOptions.raw == null && this.s.raw) newOptions.raw = this.s.raw;
  284. // Sort options
  285. if(findCommand.sort)
  286. findCommand.sort = formattedOrderClause(findCommand.sort);
  287. // Create the cursor
  288. if(typeof callback == 'function') return handleCallback(callback, null, this.s.topology.cursor(this.s.namespace, findCommand, newOptions));
  289. return this.s.topology.cursor(this.s.namespace, findCommand, newOptions);
  290. }
  291. /**
  292. * Inserts a single document into MongoDB.
  293. * @method
  294. * @param {object} doc Document to insert.
  295. * @param {object} [options=null] Optional settings.
  296. * @param {(number|string)} [options.w=null] The write concern.
  297. * @param {number} [options.wtimeout=null] The write concern timeout.
  298. * @param {boolean} [options.j=false] Specify a journal write concern.
  299. * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
  300. * @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
  301. * @param {Collection~writeOpCallback} callback The command result callback
  302. * @return {null}
  303. */
  304. Collection.prototype.insertOne = function(doc, options, callback) {
  305. if(typeof options == 'function') callback = options, options = {};
  306. if(Array.isArray(doc)) return callback(new MongoError('doc parameter must be an object'));
  307. insertDocuments(this, [doc], options, function(err, r) {
  308. if(callback == null) return;
  309. if(err && callback) return callback(err);
  310. // Workaround for pre 2.6 servers
  311. if(r == null) return callback(null, {result: {ok:1}});
  312. // Add values to top level to ensure crud spec compatibility
  313. r.insertedCount = r.result.n;
  314. r.insertedId = doc._id;
  315. if(callback) callback(null, r);
  316. });
  317. }
  318. /**
  319. * Inserts an array of documents into MongoDB.
  320. * @method
  321. * @param {object[]} docs Documents to insert.
  322. * @param {object} [options=null] Optional settings.
  323. * @param {(number|string)} [options.w=null] The write concern.
  324. * @param {number} [options.wtimeout=null] The write concern timeout.
  325. * @param {boolean} [options.j=false] Specify a journal write concern.
  326. * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
  327. * @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
  328. * @param {Collection~writeOpCallback} callback The command result callback
  329. * @return {null}
  330. */
  331. Collection.prototype.insertMany = function(docs, options, callback) {
  332. if(typeof options == 'function') callback = options, options = {};
  333. if(!Array.isArray(docs)) return callback(new MongoError('docs parameter must be an array of documents'));
  334. insertDocuments(this, docs, options, function(err, r) {
  335. if(callback == null) return;
  336. if(err && callback) return callback(err);
  337. if(r == null) return callback(null, {result: {ok:1}});
  338. r.insertedCount = r.result.n;
  339. var ids = [];
  340. for(var i = 0; i < docs.length; i++) {
  341. if(docs[i]._id) ids.push(docs[i]._id);
  342. }
  343. r.insertedIds = ids;
  344. if(callback) callback(null, r);
  345. });
  346. }
  347. /**
  348. * @typedef {Object} Collection~BulkWriteOpResult
  349. * @property {number} insertedCount Number of documents inserted.
  350. * @property {number} matchedCount Number of documents matched for update.
  351. * @property {number} modifiedCount Number of documents modified.
  352. * @property {number} deletedCount Number of documents deleted.
  353. * @property {number} upsertedCount Number of documents upserted.
  354. * @property {object} insertedIds Inserted document generated Id's, hash key is the index of the originating operation
  355. * @property {object} upsertedIds Upserted document generated Id's, hash key is the index of the originating operation
  356. * @property {object} result The command result object.
  357. */
  358. /**
  359. * The callback format for inserts
  360. * @callback Collection~bulkWriteOpCallback
  361. * @param {MongoError} error An error instance representing the error during the execution.
  362. * @param {Collection~BulkWriteOpResult} result The result object if the command was executed successfully.
  363. */
  364. /**
  365. * Perform a bulkWrite operation without a fluent API
  366. *
  367. * Legal operation types are
  368. *
  369. * { insertOne: { document: { a: 1 } } }
  370. *
  371. * { updateOne: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } }
  372. *
  373. * { updateMany: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } }
  374. *
  375. * { deleteOne: { filter: {c:1} } }
  376. *
  377. * { deleteMany: { filter: {c:1} } }
  378. *
  379. * { replaceOne: { filter: {c:3}, replacement: {c:4}, upsert:true}}
  380. *
  381. * @method
  382. * @param {object[]} operations Bulk operations to perform.
  383. * @param {object} [options=null] Optional settings.
  384. * @param {(number|string)} [options.w=null] The write concern.
  385. * @param {number} [options.wtimeout=null] The write concern timeout.
  386. * @param {boolean} [options.j=false] Specify a journal write concern.
  387. * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
  388. * @param {Collection~bulkWriteOpCallback} callback The command result callback
  389. * @return {null}
  390. */
  391. Collection.prototype.bulkWrite = function(operations, options, callback) {
  392. if(typeof options == 'function') callback = options, options = {};
  393. if(typeof callback != 'function') throw new MongoError("bulkWrite must have a callback function specified");
  394. if(!Array.isArray(operations)) throw new MongoError("operations must be an array of documents");
  395. var bulk = options.ordered == true || options.ordered == null ? this.initializeOrderedBulkOp() : this.initializeUnorderedBulkOp();
  396. // for each op go through and add to the bulk
  397. for(var i = 0; i < operations.length; i++) {
  398. bulk.raw(operations[i]);
  399. }
  400. // Final options for write concern
  401. var finalOptions = writeConcern(shallowClone(options), this.s.db, this, options);
  402. var writeCon = finalOptions.writeConcern ? finalOptions.writeConcern : {};
  403. // Execute the bulk
  404. bulk.execute(writeCon, function(err, r) {
  405. r.insertedCount = r.nInserted;
  406. r.matchedCount = r.nMatched;
  407. r.modifiedCount = r.nModified || 0;
  408. r.deletedCount = r.nRemoved;
  409. r.upsertedCount = r.getUpsertedIds().length;
  410. r.upsertedIds = {};
  411. r.insertedIds = {};
  412. // Inserted documents
  413. var inserted = r.getInsertedIds();
  414. // Map inserted ids
  415. for(var i = 0; i < inserted.length; i++) {
  416. r.insertedIds[inserted[i].index] = inserted[i]._id;
  417. }
  418. // Upserted documents
  419. var upserted = r.getUpsertedIds();
  420. // Map upserted ids
  421. for(var i = 0; i < upserted.length; i++) {
  422. r.upsertedIds[upserted[i].index] = upserted[i]._id;
  423. }
  424. // Return the results
  425. callback(null, r);
  426. });
  427. }
  428. var insertDocuments = function(self, docs, options, callback) {
  429. if(typeof options == 'function') callback = options, options = {};
  430. options = options || {};
  431. // Ensure we are operating on an array op docs
  432. docs = Array.isArray(docs) ? docs : [docs];
  433. // Get the write concern options
  434. var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
  435. if(typeof finalOptions.checkKeys != 'boolean') finalOptions.checkKeys = true;
  436. // If keep going set unordered
  437. if(options.keepGoing == true) finalOptions.ordered = false;
  438. finalOptions['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
  439. // Add _id if not specified
  440. for(var i = 0; i < docs.length; i++) {
  441. if(docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
  442. }
  443. // File inserts
  444. self.s.topology.insert(self.s.namespace, docs, finalOptions, function(err, result) {
  445. if(callback == null) return;
  446. if(err) return handleCallback(callback, err);
  447. if(result == null) return handleCallback(callback, null, null);
  448. if(result.result.code) return handleCallback(callback, toError(result.result));
  449. if(result.result.writeErrors) return handleCallback(callback, toError(result.result.writeErrors[0]));
  450. // Add docs to the list
  451. result.ops = docs;
  452. // Return the results
  453. handleCallback(callback, null, result);
  454. });
  455. }
  456. /**
  457. * @typedef {Object} Collection~WriteOpResult
  458. * @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
  459. * @property {object} connection The connection object used for the operation.
  460. * @property {object} result The command result object.
  461. */
  462. /**
  463. * The callback format for inserts
  464. * @callback Collection~writeOpCallback
  465. * @param {MongoError} error An error instance representing the error during the execution.
  466. * @param {Collection~WriteOpResult} result The result object if the command was executed successfully.
  467. */
  468. /**
  469. * Inserts a single document or a an array of documents into MongoDB.
  470. * @method
  471. * @param {(object|object[])} docs Documents to insert.
  472. * @param {object} [options=null] Optional settings.
  473. * @param {(number|string)} [options.w=null] The write concern.
  474. * @param {number} [options.wtimeout=null] The write concern timeout.
  475. * @param {boolean} [options.j=false] Specify a journal write concern.
  476. * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
  477. * @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
  478. * @param {Collection~writeOpCallback} callback The command result callback
  479. * @return {null}
  480. * @deprecated Use insertOne, insertMany or bulkWrite
  481. */
  482. Collection.prototype.insert = function(docs, options, callback) {
  483. return insertDocuments(this, docs, options, callback);
  484. }
  485. /**
  486. * Update a single document on MongoDB
  487. * @method
  488. * @param {object} filter The Filter used to select the document to update
  489. * @param {object} update The update operations to be applied to the document
  490. * @param {object} [options=null] Optional settings.
  491. * @param {boolean} [options.upsert=false] Update operation is an upsert.
  492. * @param {(number|string)} [options.w=null] The write concern.
  493. * @param {number} [options.wtimeout=null] The write concern timeout.
  494. * @param {boolean} [options.j=false] Specify a journal write concern.
  495. * @param {Collection~writeOpCallback} callback The command result callback
  496. * @return {null}
  497. */
  498. Collection.prototype.updateOne = function(filter, update, options, callback) {
  499. if(typeof options == 'function') callback = options, options = {};
  500. options = shallowClone(options)
  501. // Set single document update
  502. options.multi = false;
  503. // Execute update
  504. updateDocuments(this, filter, update, options, function(err, r) {
  505. if(callback == null) return;
  506. if(err && callback) return callback(err);
  507. if(r == null) return callback(null, {result: {ok:1}});
  508. r.matchedCount = r.result.n;
  509. r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
  510. r.upsertedId = Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? r.result.upserted[0] : null;
  511. r.upsertedCount = Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
  512. if(callback) callback(null, r);
  513. });
  514. }
  515. /**
  516. * Replace a document on MongoDB
  517. * @method
  518. * @param {object} filter The Filter used to select the document to update
  519. * @param {object} doc The Document that replaces the matching document
  520. * @param {object} [options=null] Optional settings.
  521. * @param {boolean} [options.upsert=false] Update operation is an upsert.
  522. * @param {(number|string)} [options.w=null] The write concern.
  523. * @param {number} [options.wtimeout=null] The write concern timeout.
  524. * @param {boolean} [options.j=false] Specify a journal write concern.
  525. * @param {Collection~writeOpCallback} callback The command result callback
  526. * @return {null}
  527. */
  528. Collection.prototype.replaceOne = function(filter, update, options, callback) {
  529. if(typeof options == 'function') callback = options, options = {};
  530. options = shallowClone(options)
  531. // Set single document update
  532. options.multi = false;
  533. // Execute update
  534. updateDocuments(this, filter, update, options, function(err, r) {
  535. if(callback == null) return;
  536. if(err && callback) return callback(err);
  537. if(r == null) return callback(null, {result: {ok:1}});
  538. r.matchedCount = r.result.n;
  539. r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
  540. r.upsertedId = Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? r.result.upserted[0] : null;
  541. r.upsertedCount = Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
  542. r.ops = [update];
  543. if(callback) callback(null, r);
  544. });
  545. }
  546. /**
  547. * Update multiple documents on MongoDB
  548. * @method
  549. * @param {object} filter The Filter used to select the document to update
  550. * @param {object} update The update operations to be applied to the document
  551. * @param {object} [options=null] Optional settings.
  552. * @param {boolean} [options.upsert=false] Update operation is an upsert.
  553. * @param {(number|string)} [options.w=null] The write concern.
  554. * @param {number} [options.wtimeout=null] The write concern timeout.
  555. * @param {boolean} [options.j=false] Specify a journal write concern.
  556. * @param {Collection~writeOpCallback} callback The command result callback
  557. * @return {null}
  558. */
  559. Collection.prototype.updateMany = function(filter, update, options, callback) {
  560. if(typeof options == 'function') callback = options, options = {};
  561. options = shallowClone(options)
  562. // Set single document update
  563. options.multi = true;
  564. // Execute update
  565. updateDocuments(this, filter, update, options, function(err, r) {
  566. if(callback == null) return;
  567. if(err && callback) return callback(err);
  568. if(r == null) return callback(null, {result: {ok:1}});
  569. r.matchedCount = r.result.n;
  570. r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
  571. r.upsertedId = Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? r.result.upserted[0] : null;
  572. r.upsertedCount = Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
  573. if(callback) callback(null, r);
  574. });
  575. }
  576. var updateDocuments = function(self, selector, document, options, callback) {
  577. if('function' === typeof options) callback = options, options = null;
  578. if(options == null) options = {};
  579. if(!('function' === typeof callback)) callback = null;
  580. // If we are not providing a selector or document throw
  581. if(selector == null || typeof selector != 'object') return callback(toError("selector must be a valid JavaScript object"));
  582. if(document == null || typeof document != 'object') return callback(toError("document must be a valid JavaScript object"));
  583. // Get the write concern options
  584. var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
  585. // Do we return the actual result document
  586. // Either use override on the function, or go back to default on either the collection
  587. // level or db
  588. finalOptions['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
  589. // Execute the operation
  590. var op = {q: selector, u: document};
  591. if(options.upsert) op.upsert = true;
  592. if(options.multi) op.multi = true;
  593. // Update options
  594. self.s.topology.update(self.s.namespace, [op], finalOptions, function(err, result) {
  595. if(callback == null) return;
  596. if(err) return handleCallback(callback, err, null);
  597. if(result == null) return handleCallback(callback, null, null);
  598. if(result.result.code) return handleCallback(callback, toError(result.result));
  599. if(result.result.writeErrors) return handleCallback(callback, toError(result.result.writeErrors[0]));
  600. // Return the results
  601. handleCallback(callback, null, result);
  602. });
  603. }
  604. /**
  605. * Updates documents.
  606. * @method
  607. * @param {object} selector The selector for the update operation.
  608. * @param {object} document The update document.
  609. * @param {object} [options=null] Optional settings.
  610. * @param {(number|string)} [options.w=null] The write concern.
  611. * @param {number} [options.wtimeout=null] The write concern timeout.
  612. * @param {boolean} [options.j=false] Specify a journal write concern.
  613. * @param {boolean} [options.upsert=false] Update operation is an upsert.
  614. * @param {boolean} [options.multi=false] Update one/all documents with operation.
  615. * @param {Collection~writeOpCallback} callback The command result callback
  616. * @throws {MongoError}
  617. * @return {null}
  618. * @deprecated use updateOne, updateMany or bulkWrite
  619. */
  620. Collection.prototype.update = function(selector, document, options, callback) {
  621. return updateDocuments(this, selector, document, options, callback);
  622. }
  623. /**
  624. * Delete a document on MongoDB
  625. * @method
  626. * @param {object} filter The Filter used to select the document to remove
  627. * @param {object} [options=null] Optional settings.
  628. * @param {(number|string)} [options.w=null] The write concern.
  629. * @param {number} [options.wtimeout=null] The write concern timeout.
  630. * @param {boolean} [options.j=false] Specify a journal write concern.
  631. * @param {Collection~writeOpCallback} callback The command result callback
  632. * @return {null}
  633. */
  634. Collection.prototype.deleteOne = function(filter, options, callback) {
  635. if(typeof options == 'function') callback = options, options = {};
  636. var options = shallowClone(options);
  637. options.single = true;
  638. removeDocuments(this, filter, options, function(err, r) {
  639. if(callback == null) return;
  640. if(err && callback) return callback(err);
  641. if(r == null) return callback(null, {result: {ok:1}});
  642. r.deletedCount = r.result.n;
  643. if(callback) callback(null, r);
  644. });
  645. }
  646. Collection.prototype.removeOne = Collection.prototype.deleteOne;
  647. /**
  648. * Delete multiple documents on MongoDB
  649. * @method
  650. * @param {object} filter The Filter used to select the documents to remove
  651. * @param {object} [options=null] Optional settings.
  652. * @param {(number|string)} [options.w=null] The write concern.
  653. * @param {number} [options.wtimeout=null] The write concern timeout.
  654. * @param {boolean} [options.j=false] Specify a journal write concern.
  655. * @param {Collection~writeOpCallback} callback The command result callback
  656. * @return {null}
  657. */
  658. Collection.prototype.deleteMany = function(filter, options, callback) {
  659. if(typeof options == 'function') callback = options, options = {};
  660. var options = shallowClone(options);
  661. options.single = false;
  662. removeDocuments(this, filter, options, function(err, r) {
  663. if(callback == null) return;
  664. if(err && callback) return callback(err);
  665. if(r == null) return callback(null, {result: {ok:1}});
  666. r.deletedCount = r.result.n;
  667. if(callback) callback(null, r);
  668. });
  669. }
  670. Collection.prototype.removeMany = Collection.prototype.deleteMany;
  671. var removeDocuments = function(self, selector, options, callback) {
  672. if(typeof options == 'function') {
  673. callback = options, options = {};
  674. } else if (typeof selector === 'function') {
  675. callback = selector;
  676. options = {};
  677. selector = {};
  678. }
  679. // Create an empty options object if the provided one is null
  680. options = options || {};
  681. // Get the write concern options
  682. var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
  683. // If selector is null set empty
  684. if(selector == null) selector = {};
  685. // Build the op
  686. var op = {q: selector, limit: 0};
  687. if(options.single) op.limit = 1;
  688. // Execute the remove
  689. self.s.topology.remove(self.s.namespace, [op], finalOptions, function(err, result) {
  690. if(callback == null) return;
  691. if(err) return handleCallback(callback, err, null);
  692. if(result == null) return handleCallback(callback, null, null);
  693. if(result.result.code) return handleCallback(callback, toError(result.result));
  694. if(result.result.writeErrors) return handleCallback(callback, toError(result.result.writeErrors[0]));
  695. // Return the results
  696. handleCallback(callback, null, result);
  697. });
  698. }
  699. /**
  700. * Remove documents.
  701. * @method
  702. * @param {object} selector The selector for the update operation.
  703. * @param {object} [options=null] Optional settings.
  704. * @param {(number|string)} [options.w=null] The write concern.
  705. * @param {number} [options.wtimeout=null] The write concern timeout.
  706. * @param {boolean} [options.j=false] Specify a journal write concern.
  707. * @param {boolean} [options.single=false] Removes the first document found.
  708. * @param {Collection~writeOpCallback} callback The command result callback
  709. * @return {null}
  710. * @deprecated use deleteOne, deleteMany or bulkWrite
  711. */
  712. Collection.prototype.remove = function(selector, options, callback) {
  713. return removeDocuments(this, selector, options, callback);
  714. }
  715. /**
  716. * Save a document. Simple full document replacement function. Not recommended for efficiency, use atomic
  717. * operators and update instead for more efficient operations.
  718. * @method
  719. * @param {object} doc Document to save
  720. * @param {object} [options=null] Optional settings.
  721. * @param {(number|string)} [options.w=null] The write concern.
  722. * @param {number} [options.wtimeout=null] The write concern timeout.
  723. * @param {boolean} [options.j=false] Specify a journal write concern.
  724. * @param {Collection~writeOpCallback} callback The command result callback
  725. * @return {null}
  726. */
  727. Collection.prototype.save = function(doc, options, callback) {
  728. if(typeof options == 'function') callback = options, options = {};
  729. options = options || {};
  730. // Get the write concern options
  731. var finalOptions = writeConcern(shallowClone(options), this.s.db, this, options);
  732. // Establish if we need to perform an insert or update
  733. if(doc._id != null) {
  734. finalOptions.upsert = true;
  735. return updateDocuments(this, {_id: doc._id}, doc, finalOptions, callback);
  736. }
  737. // Insert the document
  738. insertDocuments(this, [doc], options, function(err, r) {
  739. if(callback == null) return;
  740. if(doc == null) return handleCallback(callback, null, null);
  741. if(err) return handleCallback(callback, err, null);
  742. handleCallback(callback, null, r);
  743. });
  744. }
  745. /**
  746. * The callback format for results
  747. * @callback Collection~resultCallback
  748. * @param {MongoError} error An error instance representing the error during the execution.
  749. * @param {object} result The result object if the command was executed successfully.
  750. */
  751. /**
  752. * Fetches the first document that matches the query
  753. * @method
  754. * @param {object} query Query for find Operation
  755. * @param {object} [options=null] Optional settings.
  756. * @param {number} [options.limit=0] Sets the limit of documents returned in the query.
  757. * @param {(array|object)} [options.sort=null] Set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.
  758. * @param {object} [options.fields=null] The fields to return in the query. Object of fields to include or exclude (not both), {'a':1}
  759. * @param {number} [options.skip=0] Set to skip N documents ahead in your query (useful for pagination).
  760. * @param {Object} [options.hint=null] Tell the query to use specific indexes in the query. Object of indexes to use, {'_id':1}
  761. * @param {boolean} [options.explain=false] Explain the query instead of returning the data.
  762. * @param {boolean} [options.snapshot=false] Snapshot query.
  763. * @param {boolean} [options.timeout=false] Specify if the cursor can timeout.
  764. * @param {boolean} [options.tailable=false] Specify if the cursor is tailable.
  765. * @param {number} [options.batchSize=0] Set the batchSize for the getMoreCommand when iterating over the query results.
  766. * @param {boolean} [options.returnKey=false] Only return the index key.
  767. * @param {number} [options.maxScan=null] Limit the number of items to scan.
  768. * @param {number} [options.min=null] Set index bounds.
  769. * @param {number} [options.max=null] Set index bounds.
  770. * @param {boolean} [options.showDiskLoc=false] Show disk location of results.
  771. * @param {string} [options.comment=null] You can put a $comment field on a query to make looking in the profiler logs simpler.
  772. * @param {boolean} [options.raw=false] Return all BSON documents as Raw Buffer documents.
  773. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  774. * @param {boolean} [options.partial=false] Specify if the cursor should return partial results when querying against a sharded system
  775. * @param {number} [options.maxTimeMS=null] Number of miliseconds to wait before aborting the query.
  776. * @param {Collection~resultCallback} callback The command result callback
  777. * @return {null}
  778. */
  779. Collection.prototype.findOne = function() {
  780. var self = this;
  781. var args = Array.prototype.slice.call(arguments, 0);
  782. var callback = args.pop();
  783. var cursor = this.find.apply(this, args).limit(-1).batchSize(1);
  784. // Return the item
  785. cursor.next(function(err, item) {
  786. if(err != null) return handleCallback(callback, toError(err), null);
  787. handleCallback(callback, null, item);
  788. });
  789. }
  790. /**
  791. * The callback format for the collection method, must be used if strict is specified
  792. * @callback Collection~collectionResultCallback
  793. * @param {MongoError} error An error instance representing the error during the execution.
  794. * @param {Collection} collection The collection instance.
  795. */
  796. /**
  797. * Rename the collection.
  798. *
  799. * @method
  800. * @param {string} newName New name of of the collection.
  801. * @param {object} [options=null] Optional settings.
  802. * @param {boolean} [options.dropTarget=false] Drop the target name collection if it previously exists.
  803. * @param {Collection~collectionResultCallback} callback The results callback
  804. * @return {null}
  805. */
  806. Collection.prototype.rename = function(newName, opt, callback) {
  807. var self = this;
  808. if(typeof opt == 'function') callback = opt, opt = {};
  809. // Check the collection name
  810. checkCollectionName(newName);
  811. // Build the command
  812. var renameCollection = f("%s.%s", this.s.dbName, this.s.name);
  813. var toCollection = f("%s.%s", this.s.dbName, newName);
  814. var dropTarget = typeof opt.dropTarget == 'boolean' ? opt.dropTarget : false;
  815. var cmd = {'renameCollection':renameCollection, 'to':toCollection, 'dropTarget':dropTarget};
  816. // Execute against admin
  817. self.s.db.admin().command(cmd, opt, function(err, doc) {
  818. if(err) return handleCallback(callback, err, null);
  819. // We have an error
  820. if(doc.errmsg) return handleCallback(callback, toError(doc), null);
  821. try {
  822. return handleCallback(callback, null, new Collection(self.s.db, self.s.topology, self.s.dbName, newName, self.s.pkFactory, self.s.options));
  823. } catch(err) {
  824. return handleCallback(callback, toError(err), null);
  825. }
  826. });
  827. }
  828. /**
  829. * Drop the collection from the database, removing it permanently. New accesses will create a new collection.
  830. *
  831. * @method
  832. * @param {Collection~resultCallback} callback The results callback
  833. * @return {null}
  834. */
  835. Collection.prototype.drop = function(callback) {
  836. this.s.db.dropCollection(this.s.name, callback);
  837. }
  838. /**
  839. * Returns the options of the collection.
  840. *
  841. * @method
  842. * @param {Collection~resultCallback} callback The results callback
  843. * @return {null}
  844. */
  845. Collection.prototype.options = function(callback) {
  846. var self = this;
  847. this.s.db.listCollections({name: this.s.name}).toArray(function(err, collections) {
  848. if(err) return handleCallback(callback, err);
  849. if(collections.length == 0) return handleCallback(callback, new MongoError(f("collection %s not found", self.s.namespace)));
  850. handleCallback(callback, err, collections[0].options || null);
  851. });
  852. }
  853. /**
  854. * Returns if the collection is a capped collection
  855. *
  856. * @method
  857. * @param {Collection~resultCallback} callback The results callback
  858. * @return {null}
  859. */
  860. Collection.prototype.isCapped = function(callback) {
  861. this.options(function(err, document) {
  862. if(err) return handleCallback(callback, err);
  863. handleCallback(callback, null, document && document.capped);
  864. });
  865. }
  866. /**
  867. * Creates an index on the db and collection collection.
  868. * @method
  869. * @param {(string|object)} fieldOrSpec Defines the index.
  870. * @param {object} [options=null] Optional settings.
  871. * @param {(number|string)} [options.w=null] The write concern.
  872. * @param {number} [options.wtimeout=null] The write concern timeout.
  873. * @param {boolean} [options.j=false] Specify a journal write concern.
  874. * @param {boolean} [options.unique=false] Creates an unique index.
  875. * @param {boolean} [options.sparse=false] Creates a sparse index.
  876. * @param {boolean} [options.background=false] Creates the index in the background, yielding whenever possible.
  877. * @param {boolean} [options.dropDups=false] A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
  878. * @param {number} [options.min=null] For geospatial indexes set the lower bound for the co-ordinates.
  879. * @param {number} [options.max=null] For geospatial indexes set the high bound for the co-ordinates.
  880. * @param {number} [options.v=null] Specify the format version of the indexes.
  881. * @param {number} [options.expireAfterSeconds=null] Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
  882. * @param {number} [options.name=null] Override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
  883. * @param {Collection~resultCallback} callback The command result callback
  884. * @return {null}
  885. */
  886. Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
  887. var args = Array.prototype.slice.call(arguments, 1);
  888. callback = args.pop();
  889. options = args.length ? args.shift() || {} : {};
  890. options = typeof callback === 'function' ? options : callback;
  891. options = options == null ? {} : options;
  892. // Execute create index
  893. this.s.db.createIndex(this.s.name, fieldOrSpec, options, callback);
  894. }
  895. /**
  896. * Creates multiple indexes in the collection, this method is only supported for
  897. * MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported
  898. * error. Index specifications are defined at http://docs.mongodb.org/manual/reference/command/createIndexes/.
  899. * @method
  900. * @param {array} indexSpecs An array of index specifications to be created
  901. * @param {Collection~resultCallback} callback The command result callback
  902. * @return {null}
  903. */
  904. Collection.prototype.createIndexes = function(indexSpecs, callback) {
  905. // Ensure we generate the correct name if the parameter is not set
  906. for(var i = 0; i < indexSpecs.length; i++) {
  907. if(indexSpecs[i].name == null) {
  908. var keys = [];
  909. for(var name in indexSpecs[i].key) {
  910. keys.push(f('%s_%s', name, indexSpecs[i].key[name]));
  911. }
  912. // Set the name
  913. indexSpecs[i].name = keys.join('_');
  914. }
  915. }
  916. // Execute the index
  917. this.s.db.command({
  918. createIndexes: this.s.name, indexes: indexSpecs
  919. }, callback);
  920. }
  921. /**
  922. * Drops an index from this collection.
  923. * @method
  924. * @param {string} indexName Name of the index to drop.
  925. * @param {object} [options=null] Optional settings.
  926. * @param {(number|string)} [options.w=null] The write concern.
  927. * @param {number} [options.wtimeout=null] The write concern timeout.
  928. * @param {boolean} [options.j=false] Specify a journal write concern.
  929. * @param {Collection~resultCallback} callback The command result callback
  930. * @return {null}
  931. */
  932. Collection.prototype.dropIndex = function(indexName, options, callback) {
  933. var args = Array.prototype.slice.call(arguments, 1);
  934. callback = args.pop();
  935. options = args.length ? args.shift() || {} : {};
  936. // Run only against primary
  937. options.readPreference = ReadPreference.PRIMARY;
  938. // Delete index command
  939. var cmd = {'deleteIndexes':this.s.name, 'index':indexName};
  940. // Execute command
  941. this.s.db.command(cmd, options, function(err, result) {
  942. if(typeof callback != 'function') return;
  943. if(err) return handleCallback(callback, err, null);
  944. handleCallback(callback, null, result);
  945. });
  946. }
  947. /**
  948. * Drops all indexes from this collection.
  949. * @method
  950. * @param {Collection~resultCallback} callback The command result callback
  951. * @return {null}
  952. */
  953. Collection.prototype.dropIndexes = function(callback) {
  954. this.dropIndex('*', function (err, result) {
  955. if(err) return handleCallback(callback, err, false);
  956. handleCallback(callback, null, true);
  957. });
  958. }
  959. /**
  960. * Drops all indexes from this collection.
  961. * @method
  962. * @deprecated use dropIndexes
  963. * @param {Collection~resultCallback} callback The command result callback
  964. * @return {null}
  965. */
  966. Collection.prototype.dropAllIndexes = Collection.prototype.dropIndexes;
  967. /**
  968. * Reindex all indexes on the collection
  969. * Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections.
  970. * @method
  971. * @param {Collection~resultCallback} callback The command result callback
  972. * @return {null}
  973. */
  974. Collection.prototype.reIndex = function(options, callback) {
  975. if(typeof options == 'function') callback = options, options = {};
  976. options = options || {};
  977. // Reindex
  978. var cmd = {'reIndex':this.s.name};
  979. // Execute the command
  980. this.s.db.command(cmd, options, function(err, result) {
  981. if(callback == null) return;
  982. if(err) return handleCallback(callback, err, null);
  983. handleCallback(callback, null, result.ok ? true : false);
  984. });
  985. }
  986. /**
  987. * Get the list of all indexes information for the collection.
  988. *
  989. * @method
  990. * @param {object} [options=null] Optional settings.
  991. * @param {number} [options.batchSize=null] The batchSize for the returned command cursor or if pre 2.8 the systems batch collection
  992. * @return {CommandCursor}
  993. */
  994. Collection.prototype.listIndexes = function(options) {
  995. options = options || {};
  996. // Clone the options
  997. options = shallowClone(options);
  998. // Set the CommandCursor constructor
  999. options.cursorFactory = CommandCursor;
  1000. // We have a list collections command
  1001. if(this.s.db.serverConfig.capabilities().hasListIndexesCommand) {
  1002. // Cursor options
  1003. var cursor = options.batchSize ? {batchSize: options.batchSize} : {}
  1004. // Build the command
  1005. var command = { listIndexes: this.s.name, cursor: cursor };
  1006. // Execute the cursor
  1007. return this.s.topology.cursor(f('%s.$cmd', this.s.dbName), command, options);
  1008. }
  1009. // Get the namespace
  1010. var ns = f('%s.system.indexes', this.s.dbName);
  1011. // Get the query
  1012. return this.s.topology.cursor(ns, {find: ns, query: {ns: this.s.namespace}}, options);
  1013. };
  1014. /**
  1015. * Ensures that an index exists, if it does not it creates it
  1016. * @method
  1017. * @deprecated use createIndexes instead
  1018. * @param {(string|object)} fieldOrSpec Defines the index.
  1019. * @param {object} [options=null] Optional settings.
  1020. * @param {(number|string)} [options.w=null] The write concern.
  1021. * @param {number} [options.wtimeout=null] The write concern timeout.
  1022. * @param {boolean} [options.j=false] Specify a journal write concern.
  1023. * @param {boolean} [options.unique=false] Creates an unique index.
  1024. * @param {boolean} [options.sparse=false] Creates a sparse index.
  1025. * @param {boolean} [options.background=false] Creates the index in the background, yielding whenever possible.
  1026. * @param {boolean} [options.dropDups=false] A unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
  1027. * @param {number} [options.min=null] For geospatial indexes set the lower bound for the co-ordinates.
  1028. * @param {number} [options.max=null] For geospatial indexes set the high bound for the co-ordinates.
  1029. * @param {number} [options.v=null] Specify the format version of the indexes.
  1030. * @param {number} [options.expireAfterSeconds=null] Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
  1031. * @param {number} [options.name=null] Override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
  1032. * @param {Collection~resultCallback} callback The command result callback
  1033. * @return {null}
  1034. */
  1035. Collection.prototype.ensureIndex = function(fieldOrSpec, options, callback) {
  1036. if(typeof options == 'function') callback = options, options = {};
  1037. options = options || {};
  1038. // Execute ensureIndex
  1039. this.s.db.ensureIndex(this.s.name, fieldOrSpec, options, callback);
  1040. }
  1041. /**
  1042. * Checks if one or more indexes exist on the collection, fails on first non-existing index
  1043. * @method
  1044. * @param {(string|array)} indexes One or more index names to check.
  1045. * @param {Collection~resultCallback} callback The command result callback
  1046. * @return {null}
  1047. */
  1048. Collection.prototype.indexExists = function(indexes, callback) {
  1049. this.indexInformation(function(err, indexInformation) {
  1050. // If we have an error return
  1051. if(err != null) return handleCallback(callback, err, null);
  1052. // Let's check for the index names
  1053. if(!Array.isArray(indexes)) return handleCallback(callback, null, indexInformation[indexes] != null);
  1054. // Check in list of indexes
  1055. for(var i = 0; i < indexes.length; i++) {
  1056. if(indexInformation[indexes[i]] == null) {
  1057. return handleCallback(callback, null, false);
  1058. }
  1059. }
  1060. // All keys found return true
  1061. return handleCallback(callback, null, true);
  1062. });
  1063. }
  1064. /**
  1065. * Retrieves this collections index info.
  1066. * @method
  1067. * @param {object} [options=null] Optional settings.
  1068. * @param {boolean} [options.full=false] Returns the full raw index information.
  1069. * @param {Collection~resultCallback} callback The command result callback
  1070. * @return {null}
  1071. */
  1072. Collection.prototype.indexInformation = function(options, callback) {
  1073. // Unpack calls
  1074. var args = Array.prototype.slice.call(arguments, 0);
  1075. callback = args.pop();
  1076. options = args.length ? args.shift() || {} : {};
  1077. // Call the index information
  1078. this.s.db.indexInformation(this.s.name, options, callback);
  1079. }
  1080. /**
  1081. * The callback format for results
  1082. * @callback Collection~countCallback
  1083. * @param {MongoError} error An error instance representing the error during the execution.
  1084. * @param {number} result The count of documents that matched the query.
  1085. */
  1086. /**
  1087. * Count number of matching documents in the db to a query.
  1088. * @method
  1089. * @param {object} query The query for the count.
  1090. * @param {object} [options=null] Optional settings.
  1091. * @param {boolean} [options.limit=null] The limit of documents to count.
  1092. * @param {boolean} [options.skip=null] The number of documents to skip for the count.
  1093. * @param {string} [options.hint=null] An index name hint for the query.
  1094. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1095. * @param {Collection~countCallback} callback The command result callback
  1096. * @return {null}
  1097. */
  1098. Collection.prototype.count = function(query, options, callback) {
  1099. var args = Array.prototype.slice.call(arguments, 0);
  1100. callback = args.pop();
  1101. query = args.length ? args.shift() || {} : {};
  1102. options = args.length ? args.shift() || {} : {};
  1103. var skip = options.skip;
  1104. var limit = options.limit;
  1105. var hint = options.hint;
  1106. var maxTimeMS = options.maxTimeMS;
  1107. // Final query
  1108. var cmd = {
  1109. 'count': this.s.name, 'query': query
  1110. , 'fields': null
  1111. };
  1112. // Add limit and skip if defined
  1113. if(typeof skip == 'number') cmd.skip = skip;
  1114. if(typeof limit == 'number') cmd.limit = limit;
  1115. if(hint) options.hint = hint;
  1116. // Ensure we have the right read preference inheritance
  1117. options = getReadPreference(this, options, this.s.db, this);
  1118. // Execute command
  1119. this.s.db.command(cmd, options, function(err, result) {
  1120. if(err) return handleCallback(callback, err);
  1121. handleCallback(callback, null, result.n);
  1122. });
  1123. };
  1124. /**
  1125. * The distinct command returns returns a list of distinct values for the given key across a collection.
  1126. * @method
  1127. * @param {string} key Field of the document to find distinct values for.
  1128. * @param {object} query The query for filtering the set of documents to which we apply the distinct filter.
  1129. * @param {object} [options=null] Optional settings.
  1130. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1131. * @param {Collection~resultCallback} callback The command result callback
  1132. * @return {null}
  1133. */
  1134. Collection.prototype.distinct = function(key, query, options, callback) {
  1135. var args = Array.prototype.slice.call(arguments, 1);
  1136. callback = args.pop();
  1137. query = args.length ? args.shift() || {} : {};
  1138. options = args.length ? args.shift() || {} : {};
  1139. // maxTimeMS option
  1140. var maxTimeMS = options.maxTimeMS;
  1141. // Distinct command
  1142. var cmd = {
  1143. 'distinct': this.s.name, 'key': key, 'query': query
  1144. };
  1145. // Ensure we have the right read preference inheritance
  1146. options = getReadPreference(this, options, this.s.db, this);
  1147. // Execute the command
  1148. this.s.db.command(cmd, options, function(err, result) {
  1149. if(err) return handleCallback(callback, err);
  1150. handleCallback(callback, null, result.values);
  1151. });
  1152. };
  1153. /**
  1154. * Retrieve all the indexes on the collection.
  1155. * @method
  1156. * @param {Collection~resultCallback} callback The command result callback
  1157. * @return {null}
  1158. */
  1159. Collection.prototype.indexes = function(callback) {
  1160. this.s.db.indexInformation(this.s.name, {full:true}, callback);
  1161. }
  1162. /**
  1163. * Get all the collection statistics.
  1164. *
  1165. * @method
  1166. * @param {object} [options=null] Optional settings.
  1167. * @param {number} [options.scale=null] Divide the returned sizes by scale value.
  1168. * @param {Collection~resultCallback} callback The collection result callback
  1169. * @return {null}
  1170. */
  1171. Collection.prototype.stats = function(options, callback) {
  1172. var args = Array.prototype.slice.call(arguments, 0);
  1173. callback = args.pop();
  1174. // Fetch all commands
  1175. options = args.length ? args.shift() || {} : {};
  1176. // Build command object
  1177. var commandObject = {
  1178. collStats:this.s.name
  1179. }
  1180. // Check if we have the scale value
  1181. if(options['scale'] != null) commandObject['scale'] = options['scale'];
  1182. // Ensure we have the right read preference inheritance
  1183. options = getReadPreference(this, options, this.s.db, this);
  1184. // Execute the command
  1185. this.s.db.command(commandObject, options, callback);
  1186. }
  1187. /**
  1188. * Find a document and delete it in one atomic operation, requires a write lock for the duration of the operation.
  1189. *
  1190. * @method
  1191. * @param {object} filter Document selection filter.
  1192. * @param {object} [options=null] Optional settings.
  1193. * @param {object} [options.projection=null] Limits the fields to return for all matching documents.
  1194. * @param {object} [options.sort=null] Determines which document the operation modifies if the query selects multiple documents.
  1195. * @param {number} [options.maxTimeMS=null] The maximum amount of time to allow the query to run.
  1196. * @param {Collection~resultCallback} callback The collection result callback
  1197. * @return {null}
  1198. */
  1199. Collection.prototype.findOneAndDelete = function(filter, options, callback) {
  1200. if(typeof options == 'function') callback = options, options = {};
  1201. this.findAndModify(
  1202. filter
  1203. , options.sort
  1204. , null
  1205. , {
  1206. fields: options.projection
  1207. , remove:true
  1208. }
  1209. , callback
  1210. );
  1211. }
  1212. /**
  1213. * Find a document and replace it in one atomic operation, requires a write lock for the duration of the operation.
  1214. *
  1215. * @method
  1216. * @param {object} filter Document selection filter.
  1217. * @param {object} replacement Document replacing the matching document.
  1218. * @param {object} [options=null] Optional settings.
  1219. * @param {object} [options.projection=null] Limits the fields to return for all matching documents.
  1220. * @param {object} [options.sort=null] Determines which document the operation modifies if the query selects multiple documents.
  1221. * @param {number} [options.maxTimeMS=null] The maximum amount of time to allow the query to run.
  1222. * @param {boolean} [options.upsert=false] Upsert the document if it does not exist.
  1223. * @param {boolean} [options.returnOriginal=true] When false, returns the updated document rather than the original. The default is true.
  1224. * @param {Collection~resultCallback} callback The collection result callback
  1225. * @return {null}
  1226. */
  1227. Collection.prototype.findOneAndReplace = function(filter, replacement, options, callback) {
  1228. if(typeof options == 'function') callback = options, options = {};
  1229. this.findAndModify(
  1230. filter
  1231. , options.sort
  1232. , replacement
  1233. , {
  1234. fields: options.projection
  1235. , update: true
  1236. , new: typeof options.returnOriginal == 'boolean' ? !options.returnOriginal : false
  1237. , upsert: typeof options.upsert == 'boolean' ? options.upsert : false
  1238. }
  1239. , callback
  1240. );
  1241. }
  1242. /**
  1243. * Find a document and update it in one atomic operation, requires a write lock for the duration of the operation.
  1244. *
  1245. * @method
  1246. * @param {object} filter Document selection filter.
  1247. * @param {object} update Update operations to be performed on the document
  1248. * @param {object} [options=null] Optional settings.
  1249. * @param {object} [options.projection=null] Limits the fields to return for all matching documents.
  1250. * @param {object} [options.sort=null] Determines which document the operation modifies if the query selects multiple documents.
  1251. * @param {number} [options.maxTimeMS=null] The maximum amount of time to allow the query to run.
  1252. * @param {boolean} [options.upsert=false] Upsert the document if it does not exist.
  1253. * @param {boolean} [options.returnOriginal=true] When false, returns the updated document rather than the original. The default is true.
  1254. * @param {Collection~resultCallback} callback The collection result callback
  1255. * @return {null}
  1256. */
  1257. Collection.prototype.findOneAndUpdate = function(filter, update, options, callback) {
  1258. if(typeof options == 'function') callback = options, options = {};
  1259. this.findAndModify(
  1260. filter
  1261. , options.sort
  1262. , update
  1263. , {
  1264. fields: options.projection
  1265. , update: true
  1266. , new: typeof options.returnOriginal == 'boolean' ? !options.returnOriginal : false
  1267. , upsert: typeof options.upsert == 'boolean' ? options.upsert : false
  1268. }
  1269. , callback
  1270. );
  1271. }
  1272. /**
  1273. * Find and update a document.
  1274. * @method
  1275. * @param {object} query Query object to locate the object to modify.
  1276. * @param {array} sort If multiple docs match, choose the first one in the specified sort order as the object to manipulate.
  1277. * @param {object} doc The fields/vals to be updated.
  1278. * @param {object} [options=null] Optional settings.
  1279. * @param {(number|string)} [options.w=null] The write concern.
  1280. * @param {number} [options.wtimeout=null] The write concern timeout.
  1281. * @param {boolean} [options.j=false] Specify a journal write concern.
  1282. * @param {boolean} [options.remove=false] Set to true to remove the object before returning.
  1283. * @param {boolean} [options.upsert=false] Perform an upsert operation.
  1284. * @param {boolean} [options.new=false] Set to true if you want to return the modified object rather than the original. Ignored for remove.
  1285. * @param {object} [options.fields=null] Object containing the field projection for the result returned from the operation.
  1286. * @param {Collection~resultCallback} callback The command result callback
  1287. * @return {null}
  1288. * @deprecated use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead
  1289. */
  1290. Collection.prototype.findAndModify = function(query, sort, doc, options, callback) {
  1291. var args = Array.prototype.slice.call(arguments, 1);
  1292. callback = args.pop();
  1293. sort = args.length ? args.shift() || [] : [];
  1294. doc = args.length ? args.shift() : null;
  1295. options = args.length ? args.shift() || {} : {};
  1296. // Clone options
  1297. var options = shallowClone(options);
  1298. // Force read preference primary
  1299. options.readPreference = ReadPreference.PRIMARY;
  1300. // Create findAndModify command object
  1301. var queryObject = {
  1302. 'findandmodify': this.s.name
  1303. , 'query': query
  1304. };
  1305. sort = formattedOrderClause(sort);
  1306. if(sort) {
  1307. queryObject.sort = sort;
  1308. }
  1309. queryObject.new = options.new ? true : false;
  1310. queryObject.remove = options.remove ? true : false;
  1311. queryObject.upsert = options.upsert ? true : false;
  1312. if(options.fields) {
  1313. queryObject.fields = options.fields;
  1314. }
  1315. if(doc && !options.remove) {
  1316. queryObject.update = doc;
  1317. }
  1318. // Either use override on the function, or go back to default on either the collection
  1319. // level or db
  1320. if(options['serializeFunctions'] != null) {
  1321. options['serializeFunctions'] = options['serializeFunctions'];
  1322. } else {
  1323. options['serializeFunctions'] = this.s.serializeFunctions;
  1324. }
  1325. // No check on the documents
  1326. options.checkKeys = false;
  1327. // Execute the command
  1328. this.s.db.command(queryObject
  1329. , options, function(err, result) {
  1330. if(err) return handleCallback(callback, err, null);
  1331. return handleCallback(callback, null, result);
  1332. });
  1333. }
  1334. /**
  1335. * Find and remove a document.
  1336. * @method
  1337. * @param {object} query Query object to locate the object to modify.
  1338. * @param {array} sort If multiple docs match, choose the first one in the specified sort order as the object to manipulate.
  1339. * @param {object} [options=null] Optional settings.
  1340. * @param {(number|string)} [options.w=null] The write concern.
  1341. * @param {number} [options.wtimeout=null] The write concern timeout.
  1342. * @param {boolean} [options.j=false] Specify a journal write concern.
  1343. * @param {Collection~resultCallback} callback The command result callback
  1344. * @return {null}
  1345. * @deprecated use findOneAndDelete instead
  1346. */
  1347. Collection.prototype.findAndRemove = function(query, sort, options, callback) {
  1348. var args = Array.prototype.slice.call(arguments, 1);
  1349. callback = args.pop();
  1350. sort = args.length ? args.shift() || [] : [];
  1351. options = args.length ? args.shift() || {} : {};
  1352. // Add the remove option
  1353. options['remove'] = true;
  1354. // Execute the callback
  1355. this.findAndModify(query, sort, null, options, callback);
  1356. }
  1357. /**
  1358. * Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.2
  1359. * @method
  1360. * @param {object} pipeline Array containing all the aggregation framework commands for the execution.
  1361. * @param {object} [options=null] Optional settings.
  1362. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1363. * @param {object} [options.cursor=null] Return the query as cursor, on 2.6 > it returns as a real cursor on pre 2.6 it returns as an emulated cursor.
  1364. * @param {number} [options.cursor.batchSize=null] The batchSize for the cursor
  1365. * @param {boolean} [options.explain=false] Explain returns the aggregation execution plan (requires mongodb 2.6 >).
  1366. * @param {boolean} [options.allowDiskUse=false] allowDiskUse lets the server know if it can use disk to store temporary results for the aggregation (requires mongodb 2.6 >).
  1367. * @param {number} [options.maxTimeMS=null] maxTimeMS specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point.
  1368. * @param {Collection~resultCallback} callback The command result callback
  1369. * @return {(null|AggregationCursor)}
  1370. */
  1371. Collection.prototype.aggregate = function(pipeline, options, callback) {
  1372. if(Array.isArray(pipeline)) {
  1373. // Set up callback if one is provided
  1374. if(typeof options == 'function') {
  1375. callback = options;
  1376. options = {};
  1377. }
  1378. // If we have no options or callback we are doing
  1379. // a cursor based aggregation
  1380. if(options == null && callback == null) {
  1381. options = {};
  1382. }
  1383. } else {
  1384. // Aggregation pipeline passed as arguments on the method
  1385. var args = Array.prototype.slice.call(arguments, 0);
  1386. // Get the callback
  1387. callback = args.pop();
  1388. // Get the possible options object
  1389. var opts = args[args.length - 1];
  1390. // If it contains any of the admissible options pop it of the args
  1391. options = opts && (opts.readPreference
  1392. || opts.explain || opts.cursor || opts.out
  1393. || opts.maxTimeMS || opts.allowDiskUse) ? args.pop() : {};
  1394. // Left over arguments is the pipeline
  1395. pipeline = args;
  1396. }
  1397. // If out was specified
  1398. if(typeof options.out == 'string') {
  1399. pipeline.push({$out: options.out});
  1400. }
  1401. // Build the command
  1402. var command = { aggregate : this.s.name, pipeline : pipeline};
  1403. // If we have allowDiskUse defined
  1404. if(options.allowDiskUse) command.allowDiskUse = options.allowDiskUse;
  1405. if(typeof options.maxTimeMS == 'number') command.maxTimeMS = options.maxTimeMS;
  1406. // Ensure we have the right read preference inheritance
  1407. options = getReadPreference(this, options, this.s.db, this);
  1408. // If explain has been specified add it
  1409. if(options.explain) command.explain = options.explain;
  1410. // Validate that cursor options is valid
  1411. if(options.cursor != null && typeof options.cursor != 'object') {
  1412. throw toError('cursor options must be an object');
  1413. }
  1414. // Set the AggregationCursor constructor
  1415. options.cursorFactory = AggregationCursor;
  1416. if(typeof callback != 'function') {
  1417. if(this.s.topology.capabilities().hasAggregationCursor) {
  1418. options.cursor = options.cursor || { batchSize : 1000 };
  1419. command.cursor = options.cursor;
  1420. }
  1421. // Allow disk usage command
  1422. if(typeof options.allowDiskUse == 'boolean') command.allowDiskUse = options.allowDiskUse;
  1423. if(typeof options.maxTimeMS == 'number') command.maxTimeMS = options.maxTimeMS;
  1424. // Execute the cursor
  1425. return this.s.topology.cursor(this.s.namespace, command, options);
  1426. }
  1427. var cursor = null;
  1428. // We do not allow cursor
  1429. if(options.cursor) {
  1430. return this.s.topology.cursor(this.s.namespace, command, options);
  1431. }
  1432. // Execute the command
  1433. this.s.db.command(command, options, function(err, result) {
  1434. if(err) {
  1435. handleCallback(callback, err);
  1436. } else if(result['err'] || result['errmsg']) {
  1437. handleCallback(callback, toError(result));
  1438. } else if(typeof result == 'object' && result['serverPipeline']) {
  1439. handleCallback(callback, null, result['serverPipeline']);
  1440. } else if(typeof result == 'object' && result['stages']) {
  1441. handleCallback(callback, null, result['stages']);
  1442. } else {
  1443. handleCallback(callback, null, result.result);
  1444. }
  1445. });
  1446. }
  1447. /**
  1448. * The callback format for results
  1449. * @callback Collection~parallelCollectionScanCallback
  1450. * @param {MongoError} error An error instance representing the error during the execution.
  1451. * @param {Cursor[]} cursors A list of cursors returned allowing for parallel reading of collection.
  1452. */
  1453. /**
  1454. * Return N number of parallel cursors for a collection allowing parallel reading of entire collection. There are
  1455. * no ordering guarantees for returned results.
  1456. * @method
  1457. * @param {object} [options=null] Optional settings.
  1458. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1459. * @param {number} [options.batchSize=null] Set the batchSize for the getMoreCommand when iterating over the query results.
  1460. * @param {number} [options.numCursors=1] The maximum number of parallel command cursors to return (the number of returned cursors will be in the range 1:numCursors)
  1461. * @param {boolean} [options.raw=false] Return all BSON documents as Raw Buffer documents.
  1462. * @param {Collection~parallelCollectionScanCallback} callback The command result callback
  1463. * @return {null}
  1464. */
  1465. Collection.prototype.parallelCollectionScan = function(options, callback) {
  1466. var self = this;
  1467. if(typeof options == 'function') callback = options, options = {numCursors: 1};
  1468. // Set number of cursors to 1
  1469. options.numCursors = options.numCursors || 1;
  1470. options.batchSize = options.batchSize || 1000;
  1471. // Ensure we have the right read preference inheritance
  1472. options = getReadPreference(this, options, this.s.db, this);
  1473. // Create command object
  1474. var commandObject = {
  1475. parallelCollectionScan: this.s.name
  1476. , numCursors: options.numCursors
  1477. }
  1478. // Execute the command
  1479. this.s.db.command(commandObject, options, function(err, result) {
  1480. if(err) return handleCallback(callback, err, null);
  1481. if(result == null) return handleCallback(callback, new Error("no result returned for parallelCollectionScan"), null);
  1482. var cursors = [];
  1483. // Create command cursors for each item
  1484. for(var i = 0; i < result.cursors.length; i++) {
  1485. var rawId = result.cursors[i].cursor.id
  1486. // Convert cursorId to Long if needed
  1487. var cursorId = typeof rawId == 'number' ? Long.fromNumber(rawId) : rawId;
  1488. // Command cursor options
  1489. var cmd = {
  1490. batchSize: options.batchSize
  1491. , cursorId: cursorId
  1492. , items: result.cursors[i].cursor.firstBatch
  1493. }
  1494. // Add a command cursor
  1495. cursors.push(self.s.topology.cursor(self.s.namespace, cursorId, options));
  1496. }
  1497. handleCallback(callback, null, cursors);
  1498. });
  1499. }
  1500. /**
  1501. * Execute the geoNear command to search for items in the collection
  1502. *
  1503. * @method
  1504. * @param {number} x Point to search on the x axis, ensure the indexes are ordered in the same order.
  1505. * @param {number} y Point to search on the y axis, ensure the indexes are ordered in the same order.
  1506. * @param {object} [options=null] Optional settings.
  1507. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1508. * @param {number} [options.num=null] Max number of results to return.
  1509. * @param {number} [options.minDistance=null] Include results starting at minDistance from a point (2.6 or higher)
  1510. * @param {number} [options.maxDistance=null] Include results up to maxDistance from the point.
  1511. * @param {number} [options.distanceMultiplier=null] Include a value to multiply the distances with allowing for range conversions.
  1512. * @param {object} [options.query=null] Filter the results by a query.
  1513. * @param {boolean} [options.spherical=false] Perform query using a spherical model.
  1514. * @param {boolean} [options.uniqueDocs=false] The closest location in a document to the center of the search region will always be returned MongoDB > 2.X.
  1515. * @param {boolean} [options.includeLocs=false] Include the location data fields in the top level of the results MongoDB > 2.X.
  1516. * @param {Collection~resultCallback} callback The command result callback
  1517. * @return {null}
  1518. */
  1519. Collection.prototype.geoNear = function(x, y, options, callback) {
  1520. var point = typeof(x) == 'object' && x
  1521. , args = Array.prototype.slice.call(arguments, point?1:2);
  1522. callback = args.pop();
  1523. // Fetch all commands
  1524. options = args.length ? args.shift() || {} : {};
  1525. // Build command object
  1526. var commandObject = {
  1527. geoNear:this.s.name,
  1528. near: point || [x, y]
  1529. }
  1530. // Ensure we have the right read preference inheritance
  1531. options = getReadPreference(this, options, this.s.db, this);
  1532. // Exclude readPreference and existing options to prevent user from
  1533. // shooting themselves in the foot
  1534. var exclude = {
  1535. readPreference: true,
  1536. geoNear: true,
  1537. near: true
  1538. };
  1539. // Filter out any excluded objects
  1540. commandObject = decorateCommand(commandObject, options, exclude);
  1541. // Execute the command
  1542. this.s.db.command(commandObject, options, function (err, res) {
  1543. if(err) return handleCallback(callback, err);
  1544. if(res.err || res.errmsg) return handleCallback(callback, toError(res));
  1545. // should we only be returning res.results here? Not sure if the user
  1546. // should see the other return information
  1547. handleCallback(callback, null, res);
  1548. });
  1549. }
  1550. /**
  1551. * Execute a geo search using a geo haystack index on a collection.
  1552. *
  1553. * @method
  1554. * @param {number} x Point to search on the x axis, ensure the indexes are ordered in the same order.
  1555. * @param {number} y Point to search on the y axis, ensure the indexes are ordered in the same order.
  1556. * @param {object} [options=null] Optional settings.
  1557. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1558. * @param {number} [options.maxDistance=null] Include results up to maxDistance from the point.
  1559. * @param {object} [options.search=null] Filter the results by a query.
  1560. * @param {number} [options.limit=false] Max number of results to return.
  1561. * @param {Collection~resultCallback} callback The command result callback
  1562. * @return {null}
  1563. */
  1564. Collection.prototype.geoHaystackSearch = function(x, y, options, callback) {
  1565. var args = Array.prototype.slice.call(arguments, 2);
  1566. callback = args.pop();
  1567. // Fetch all commands
  1568. options = args.length ? args.shift() || {} : {};
  1569. // Build command object
  1570. var commandObject = {
  1571. geoSearch: this.s.name,
  1572. near: [x, y]
  1573. }
  1574. // Remove read preference from hash if it exists
  1575. commandObject = decorateCommand(commandObject, options, {readPreference: true});
  1576. // Ensure we have the right read preference inheritance
  1577. options = getReadPreference(this, options, this.s.db, this);
  1578. // Execute the command
  1579. this.s.db.command(commandObject, options, function (err, res) {
  1580. if(err) return handleCallback(callback, err);
  1581. if(res.err || res.errmsg) handleCallback(callback, utils.toError(res));
  1582. // should we only be returning res.results here? Not sure if the user
  1583. // should see the other return information
  1584. handleCallback(callback, null, res);
  1585. });
  1586. }
  1587. /**
  1588. * Group function helper
  1589. * @ignore
  1590. */
  1591. var groupFunction = function () {
  1592. var c = db[ns].find(condition);
  1593. var map = new Map();
  1594. var reduce_function = reduce;
  1595. while (c.hasNext()) {
  1596. var obj = c.next();
  1597. var key = {};
  1598. for (var i = 0, len = keys.length; i < len; ++i) {
  1599. var k = keys[i];
  1600. key[k] = obj[k];
  1601. }
  1602. var aggObj = map.get(key);
  1603. if (aggObj == null) {
  1604. var newObj = Object.extend({}, key);
  1605. aggObj = Object.extend(newObj, initial);
  1606. map.put(key, aggObj);
  1607. }
  1608. reduce_function(obj, aggObj);
  1609. }
  1610. return { "result": map.values() };
  1611. }.toString();
  1612. /**
  1613. * Run a group command across a collection
  1614. *
  1615. * @method
  1616. * @param {(object|array|function|code)} keys An object, array or function expressing the keys to group by.
  1617. * @param {object} condition An optional condition that must be true for a row to be considered.
  1618. * @param {object} initial Initial value of the aggregation counter object.
  1619. * @param {(function|Code)} reduce The reduce function aggregates (reduces) the objects iterated
  1620. * @param {(function|Code)} finalize An optional function to be run on each item in the result set just before the item is returned.
  1621. * @param {boolean} command Specify if you wish to run using the internal group command or using eval, default is true.
  1622. * @param {object} [options=null] Optional settings.
  1623. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1624. * @param {Collection~resultCallback} callback The command result callback
  1625. * @return {null}
  1626. */
  1627. Collection.prototype.group = function(keys, condition, initial, reduce, finalize, command, options, callback) {
  1628. var args = Array.prototype.slice.call(arguments, 3);
  1629. callback = args.pop();
  1630. // Fetch all commands
  1631. reduce = args.length ? args.shift() : null;
  1632. finalize = args.length ? args.shift() : null;
  1633. command = args.length ? args.shift() : null;
  1634. options = args.length ? args.shift() || {} : {};
  1635. // Make sure we are backward compatible
  1636. if(!(typeof finalize == 'function')) {
  1637. command = finalize;
  1638. finalize = null;
  1639. }
  1640. if (!Array.isArray(keys) && keys instanceof Object && typeof(keys) !== 'function' && !(keys instanceof Code)) {
  1641. keys = Object.keys(keys);
  1642. }
  1643. if(typeof reduce === 'function') {
  1644. reduce = reduce.toString();
  1645. }
  1646. if(typeof finalize === 'function') {
  1647. finalize = finalize.toString();
  1648. }
  1649. // Set up the command as default
  1650. command = command == null ? true : command;
  1651. // Execute using the command
  1652. if(command) {
  1653. var reduceFunction = reduce instanceof Code
  1654. ? reduce
  1655. : new Code(reduce);
  1656. var selector = {
  1657. group: {
  1658. 'ns': this.s.name
  1659. , '$reduce': reduceFunction
  1660. , 'cond': condition
  1661. , 'initial': initial
  1662. , 'out': "inline"
  1663. }
  1664. };
  1665. // if finalize is defined
  1666. if(finalize != null) selector.group['finalize'] = finalize;
  1667. // Set up group selector
  1668. if ('function' === typeof keys || keys instanceof Code) {
  1669. selector.group.$keyf = keys instanceof Code
  1670. ? keys
  1671. : new Code(keys);
  1672. } else {
  1673. var hash = {};
  1674. keys.forEach(function (key) {
  1675. hash[key] = 1;
  1676. });
  1677. selector.group.key = hash;
  1678. }
  1679. // Ensure we have the right read preference inheritance
  1680. options = getReadPreference(this, options, this.s.db, this);
  1681. // Execute command
  1682. this.s.db.command(selector, options, function(err, result) {
  1683. if(err) return handleCallback(callback, err, null);
  1684. handleCallback(callback, null, result.retval);
  1685. });
  1686. } else {
  1687. // Create execution scope
  1688. var scope = reduce != null && reduce instanceof Code
  1689. ? reduce.scope
  1690. : {};
  1691. scope.ns = this.s.name;
  1692. scope.keys = keys;
  1693. scope.condition = condition;
  1694. scope.initial = initial;
  1695. // Pass in the function text to execute within mongodb.
  1696. var groupfn = groupFunction.replace(/ reduce;/, reduce.toString() + ';');
  1697. this.s.db.eval(new Code(groupfn, scope), function (err, results) {
  1698. if (err) return handleCallback(callback, err, null);
  1699. handleCallback(callback, null, results.result || results);
  1700. });
  1701. }
  1702. }
  1703. /**
  1704. * Functions that are passed as scope args must
  1705. * be converted to Code instances.
  1706. * @ignore
  1707. */
  1708. function processScope (scope) {
  1709. if(!isObject(scope)) {
  1710. return scope;
  1711. }
  1712. var keys = Object.keys(scope);
  1713. var i = keys.length;
  1714. var key;
  1715. var new_scope = {};
  1716. while (i--) {
  1717. key = keys[i];
  1718. if ('function' == typeof scope[key]) {
  1719. new_scope[key] = new Code(String(scope[key]));
  1720. } else {
  1721. new_scope[key] = processScope(scope[key]);
  1722. }
  1723. }
  1724. return new_scope;
  1725. }
  1726. /**
  1727. * Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
  1728. *
  1729. * @method
  1730. * @param {(function|string)} map The mapping function.
  1731. * @param {(function|string)} reduce The reduce function.
  1732. * @param {object} [options=null] Optional settings.
  1733. * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
  1734. * @param {object} [options.out=null] Sets the output target for the map reduce job. *{inline:1} | {replace:'collectionName'} | {merge:'collectionName'} | {reduce:'collectionName'}*
  1735. * @param {object} [options.query=null] Query filter object.
  1736. * @param {object} [options.sort=null] Sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces.
  1737. * @param {number} [options.limit=null] Number of objects to return from collection.
  1738. * @param {boolean} [options.keeptemp=false] Keep temporary data.
  1739. * @param {(function|string)} [options.finalize=null] Finalize function.
  1740. * @param {object} [options.scope=null] Can pass in variables that can be access from map/reduce/finalize.
  1741. * @param {boolean} [options.jsMode=false] It is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X.
  1742. * @param {boolean} [options.verbose=false] Provide statistics on job execution time.
  1743. * @param {Collection~resultCallback} callback The command result callback
  1744. * @throws {MongoError}
  1745. * @return {null}
  1746. */
  1747. Collection.prototype.mapReduce = function(map, reduce, options, callback) {
  1748. var self = this;
  1749. if('function' === typeof options) callback = options, options = {};
  1750. // Out must allways be defined (make sure we don't break weirdly on pre 1.8+ servers)
  1751. if(null == options.out) {
  1752. throw new Error("the out option parameter must be defined, see mongodb docs for possible values");
  1753. }
  1754. if('function' === typeof map) {
  1755. map = map.toString();
  1756. }
  1757. if('function' === typeof reduce) {
  1758. reduce = reduce.toString();
  1759. }
  1760. if('function' === typeof options.finalize) {
  1761. options.finalize = options.finalize.toString();
  1762. }
  1763. var mapCommandHash = {
  1764. mapreduce: this.s.name
  1765. , map: map
  1766. , reduce: reduce
  1767. };
  1768. // Add any other options passed in
  1769. for(var n in options) {
  1770. if('scope' == n) {
  1771. mapCommandHash[n] = processScope(options[n]);
  1772. } else {
  1773. mapCommandHash[n] = options[n];
  1774. }
  1775. }
  1776. // Ensure we have the right read preference inheritance
  1777. options = getReadPreference(this, options, this.s.db, this);
  1778. // If we have a read preference and inline is not set as output fail hard
  1779. if((options.readPreference != false && options.readPreference != 'primary')
  1780. && options['out'] && (options['out'].inline != 1 && options['out'] != 'inline')) {
  1781. options.readPreference = 'primary';
  1782. }
  1783. // Execute command
  1784. this.s.db.command(mapCommandHash, {readPreference:options.readPreference}, function (err, result) {
  1785. if(err) return handleCallback(callback, err);
  1786. // Check if we have an error
  1787. if(1 != result.ok || result.err || result.errmsg) {
  1788. return handleCallback(callback, toError(result));
  1789. }
  1790. // Create statistics value
  1791. var stats = {};
  1792. if(result.timeMillis) stats['processtime'] = result.timeMillis;
  1793. if(result.counts) stats['counts'] = result.counts;
  1794. if(result.timing) stats['timing'] = result.timing;
  1795. // invoked with inline?
  1796. if(result.results) {
  1797. // If we wish for no verbosity
  1798. if(options['verbose'] == null || !options['verbose']) {
  1799. return handleCallback(callback, null, result.results);
  1800. }
  1801. return handleCallback(callback, null, result.results, stats);
  1802. }
  1803. // The returned collection
  1804. var collection = null;
  1805. // If we have an object it's a different db
  1806. if(result.result != null && typeof result.result == 'object') {
  1807. var doc = result.result;
  1808. collection = self.s.db.db(doc.db).collection(doc.collection);
  1809. } else {
  1810. // Create a collection object that wraps the result collection
  1811. collection = self.s.db.collection(result.result)
  1812. }
  1813. // If we wish for no verbosity
  1814. if(options['verbose'] == null || !options['verbose']) {
  1815. return handleCallback(callback, err, collection);
  1816. }
  1817. // Return stats as third set of values
  1818. handleCallback(callback, err, collection, stats);
  1819. });
  1820. }
  1821. /**
  1822. * Initiate a Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order.
  1823. *
  1824. * @method
  1825. * @param {object} [options=null] Optional settings.
  1826. * @param {(number|string)} [options.w=null] The write concern.
  1827. * @param {number} [options.wtimeout=null] The write concern timeout.
  1828. * @param {boolean} [options.j=false] Specify a journal write concern.
  1829. * @param {UnorderedBulkOperation} callback The command result callback
  1830. * @return {null}
  1831. */
  1832. Collection.prototype.initializeUnorderedBulkOp = function(options) {
  1833. return unordered(this.s.topology, this, options);
  1834. }
  1835. /**
  1836. * Initiate an In order bulk write operation, operations will be serially executed in the order they are added, creating a new operation for each switch in types.
  1837. *
  1838. * @method
  1839. * @param {object} [options=null] Optional settings.
  1840. * @param {(number|string)} [options.w=null] The write concern.
  1841. * @param {number} [options.wtimeout=null] The write concern timeout.
  1842. * @param {boolean} [options.j=false] Specify a journal write concern.
  1843. * @param {OrderedBulkOperation} callback The command result callback
  1844. * @return {null}
  1845. */
  1846. Collection.prototype.initializeOrderedBulkOp = function(options) {
  1847. return ordered(this.s.topology, this, options);
  1848. }
  1849. // Get write concern
  1850. var writeConcern = function(target, db, col, options) {
  1851. if(options.w != null || options.j != null || options.fsync != null) {
  1852. var opts = {};
  1853. if(options.w != null) opts.w = options.w;
  1854. if(options.wtimeout != null) opts.wtimeout = options.wtimeout;
  1855. if(options.j != null) opts.j = options.j;
  1856. if(options.fsync != null) opts.fsync = options.fsync;
  1857. target.writeConcern = opts;
  1858. } else if(col.writeConcern.w != null || col.writeConcern.j != null || col.writeConcern.fsync != null) {
  1859. target.writeConcern = col.writeConcern;
  1860. } else if(db.writeConcern.w != null || db.writeConcern.j != null || db.writeConcern.fsync != null) {
  1861. target.writeConcern = db.writeConcern;
  1862. }
  1863. return target
  1864. }
  1865. // Figure out the read preference
  1866. var getReadPreference = function(self, options, db, coll) {
  1867. var r = null
  1868. if(options.readPreference) {
  1869. r = options.readPreference
  1870. } else if(self.s.readPreference) {
  1871. r = self.s.readPreference
  1872. } else if(db.readPreference) {
  1873. r = db.readPreference;
  1874. }
  1875. if(r instanceof ReadPreference) {
  1876. options.readPreference = new CoreReadPreference(r.mode, r.tags);
  1877. } else if(typeof r == 'string') {
  1878. options.readPreference = new CoreReadPreference(r);
  1879. }
  1880. return options;
  1881. }
  1882. var testForFields = {
  1883. limit: 1, sort: 1, fields:1, skip: 1, hint: 1, explain: 1, snapshot: 1, timeout: 1, tailable: 1, tailableRetryInterval: 1
  1884. , numberOfRetries: 1, awaitdata: 1, awaitData: 1, exhaust: 1, batchSize: 1, returnKey: 1, maxScan: 1, min: 1, max: 1, showDiskLoc: 1
  1885. , comment: 1, raw: 1, readPreference: 1, partial: 1, read: 1, dbName: 1, oplogReplay: 1, connection: 1, maxTimeMS: 1, transforms: 1
  1886. }
  1887. module.exports = Collection;