common_functions.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. 'use strict';
  2. const applyRetryableWrites = require('../utils').applyRetryableWrites;
  3. const applyWriteConcern = require('../utils').applyWriteConcern;
  4. const decorateWithCollation = require('../utils').decorateWithCollation;
  5. const decorateWithReadConcern = require('../utils').decorateWithReadConcern;
  6. const executeCommand = require('./db_ops').executeCommand;
  7. const formattedOrderClause = require('../utils').formattedOrderClause;
  8. const handleCallback = require('../utils').handleCallback;
  9. const MongoError = require('../core').MongoError;
  10. const ReadPreference = require('../core').ReadPreference;
  11. const toError = require('../utils').toError;
  12. const CursorState = require('../core/cursor').CursorState;
  13. /**
  14. * Build the count command.
  15. *
  16. * @method
  17. * @param {collectionOrCursor} an instance of a collection or cursor
  18. * @param {object} query The query for the count.
  19. * @param {object} [options] Optional settings. See Collection.prototype.count and Cursor.prototype.count for a list of options.
  20. */
  21. function buildCountCommand(collectionOrCursor, query, options) {
  22. const skip = options.skip;
  23. const limit = options.limit;
  24. let hint = options.hint;
  25. const maxTimeMS = options.maxTimeMS;
  26. query = query || {};
  27. // Final query
  28. const cmd = {
  29. count: options.collectionName,
  30. query: query
  31. };
  32. if (collectionOrCursor.s.numberOfRetries) {
  33. // collectionOrCursor is a cursor
  34. if (collectionOrCursor.options.hint) {
  35. hint = collectionOrCursor.options.hint;
  36. } else if (collectionOrCursor.cmd.hint) {
  37. hint = collectionOrCursor.cmd.hint;
  38. }
  39. decorateWithCollation(cmd, collectionOrCursor, collectionOrCursor.cmd);
  40. } else {
  41. decorateWithCollation(cmd, collectionOrCursor, options);
  42. }
  43. // Add limit, skip and maxTimeMS if defined
  44. if (typeof skip === 'number') cmd.skip = skip;
  45. if (typeof limit === 'number') cmd.limit = limit;
  46. if (typeof maxTimeMS === 'number') cmd.maxTimeMS = maxTimeMS;
  47. if (hint) cmd.hint = hint;
  48. // Do we have a readConcern specified
  49. decorateWithReadConcern(cmd, collectionOrCursor);
  50. return cmd;
  51. }
  52. function deleteCallback(err, r, callback) {
  53. if (callback == null) return;
  54. if (err && callback) return callback(err);
  55. if (r == null) return callback(null, { result: { ok: 1 } });
  56. r.deletedCount = r.result.n;
  57. if (callback) callback(null, r);
  58. }
  59. /**
  60. * Find and update a document.
  61. *
  62. * @method
  63. * @param {Collection} a Collection instance.
  64. * @param {object} query Query object to locate the object to modify.
  65. * @param {array} sort If multiple docs match, choose the first one in the specified sort order as the object to manipulate.
  66. * @param {object} doc The fields/vals to be updated.
  67. * @param {object} [options] Optional settings. See Collection.prototype.findAndModify for a list of options.
  68. * @param {Collection~findAndModifyCallback} [callback] The command result callback
  69. * @deprecated use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead
  70. */
  71. function findAndModify(coll, query, sort, doc, options, callback) {
  72. // Create findAndModify command object
  73. const queryObject = {
  74. findAndModify: coll.collectionName,
  75. query: query
  76. };
  77. sort = formattedOrderClause(sort);
  78. if (sort) {
  79. queryObject.sort = sort;
  80. }
  81. queryObject.new = options.new ? true : false;
  82. queryObject.remove = options.remove ? true : false;
  83. queryObject.upsert = options.upsert ? true : false;
  84. const projection = options.projection || options.fields;
  85. if (projection) {
  86. queryObject.fields = projection;
  87. }
  88. if (options.arrayFilters) {
  89. queryObject.arrayFilters = options.arrayFilters;
  90. delete options.arrayFilters;
  91. }
  92. if (doc && !options.remove) {
  93. queryObject.update = doc;
  94. }
  95. if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;
  96. // Either use override on the function, or go back to default on either the collection
  97. // level or db
  98. options.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
  99. // No check on the documents
  100. options.checkKeys = false;
  101. // Final options for retryable writes and write concern
  102. let finalOptions = Object.assign({}, options);
  103. finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
  104. finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
  105. // Decorate the findAndModify command with the write Concern
  106. if (finalOptions.writeConcern) {
  107. queryObject.writeConcern = finalOptions.writeConcern;
  108. }
  109. // Have we specified bypassDocumentValidation
  110. if (finalOptions.bypassDocumentValidation === true) {
  111. queryObject.bypassDocumentValidation = finalOptions.bypassDocumentValidation;
  112. }
  113. finalOptions.readPreference = ReadPreference.primary;
  114. // Have we specified collation
  115. try {
  116. decorateWithCollation(queryObject, coll, finalOptions);
  117. } catch (err) {
  118. return callback(err, null);
  119. }
  120. // Execute the command
  121. executeCommand(coll.s.db, queryObject, finalOptions, (err, result) => {
  122. if (err) return handleCallback(callback, err, null);
  123. return handleCallback(callback, null, result);
  124. });
  125. }
  126. /**
  127. * Retrieves this collections index info.
  128. *
  129. * @method
  130. * @param {Db} db The Db instance on which to retrieve the index info.
  131. * @param {string} name The name of the collection.
  132. * @param {object} [options] Optional settings. See Db.prototype.indexInformation for a list of options.
  133. * @param {Db~resultCallback} [callback] The command result callback
  134. */
  135. function indexInformation(db, name, options, callback) {
  136. // If we specified full information
  137. const full = options['full'] == null ? false : options['full'];
  138. // Did the user destroy the topology
  139. if (db.serverConfig && db.serverConfig.isDestroyed())
  140. return callback(new MongoError('topology was destroyed'));
  141. // Process all the results from the index command and collection
  142. function processResults(indexes) {
  143. // Contains all the information
  144. let info = {};
  145. // Process all the indexes
  146. for (let i = 0; i < indexes.length; i++) {
  147. const index = indexes[i];
  148. // Let's unpack the object
  149. info[index.name] = [];
  150. for (let name in index.key) {
  151. info[index.name].push([name, index.key[name]]);
  152. }
  153. }
  154. return info;
  155. }
  156. // Get the list of indexes of the specified collection
  157. db.collection(name)
  158. .listIndexes(options)
  159. .toArray((err, indexes) => {
  160. if (err) return callback(toError(err));
  161. if (!Array.isArray(indexes)) return handleCallback(callback, null, []);
  162. if (full) return handleCallback(callback, null, indexes);
  163. handleCallback(callback, null, processResults(indexes));
  164. });
  165. }
  166. function prepareDocs(coll, docs, options) {
  167. const forceServerObjectId =
  168. typeof options.forceServerObjectId === 'boolean'
  169. ? options.forceServerObjectId
  170. : coll.s.db.options.forceServerObjectId;
  171. // no need to modify the docs if server sets the ObjectId
  172. if (forceServerObjectId === true) {
  173. return docs;
  174. }
  175. return docs.map(doc => {
  176. if (forceServerObjectId !== true && doc._id == null) {
  177. doc._id = coll.s.pkFactory.createPk();
  178. }
  179. return doc;
  180. });
  181. }
  182. // Get the next available document from the cursor, returns null if no more documents are available.
  183. function nextObject(cursor, callback) {
  184. if (cursor.s.state === CursorState.CLOSED || (cursor.isDead && cursor.isDead())) {
  185. return handleCallback(
  186. callback,
  187. MongoError.create({ message: 'Cursor is closed', driver: true })
  188. );
  189. }
  190. if (cursor.s.state === CursorState.INIT && cursor.cmd && cursor.cmd.sort) {
  191. try {
  192. cursor.cmd.sort = formattedOrderClause(cursor.cmd.sort);
  193. } catch (err) {
  194. return handleCallback(callback, err);
  195. }
  196. }
  197. // Get the next object
  198. cursor._next((err, doc) => {
  199. cursor.s.state = CursorState.OPEN;
  200. if (err) return handleCallback(callback, err);
  201. handleCallback(callback, null, doc);
  202. });
  203. }
  204. function insertDocuments(coll, docs, options, callback) {
  205. if (typeof options === 'function') (callback = options), (options = {});
  206. options = options || {};
  207. // Ensure we are operating on an array op docs
  208. docs = Array.isArray(docs) ? docs : [docs];
  209. // Final options for retryable writes and write concern
  210. let finalOptions = Object.assign({}, options);
  211. finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
  212. finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
  213. // If keep going set unordered
  214. if (finalOptions.keepGoing === true) finalOptions.ordered = false;
  215. finalOptions.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
  216. docs = prepareDocs(coll, docs, options);
  217. // File inserts
  218. coll.s.topology.insert(coll.s.namespace, docs, finalOptions, (err, result) => {
  219. if (callback == null) return;
  220. if (err) return handleCallback(callback, err);
  221. if (result == null) return handleCallback(callback, null, null);
  222. if (result.result.code) return handleCallback(callback, toError(result.result));
  223. if (result.result.writeErrors)
  224. return handleCallback(callback, toError(result.result.writeErrors[0]));
  225. // Add docs to the list
  226. result.ops = docs;
  227. // Return the results
  228. handleCallback(callback, null, result);
  229. });
  230. }
  231. function removeDocuments(coll, selector, options, callback) {
  232. if (typeof options === 'function') {
  233. (callback = options), (options = {});
  234. } else if (typeof selector === 'function') {
  235. callback = selector;
  236. options = {};
  237. selector = {};
  238. }
  239. // Create an empty options object if the provided one is null
  240. options = options || {};
  241. // Final options for retryable writes and write concern
  242. let finalOptions = Object.assign({}, options);
  243. finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
  244. finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
  245. // If selector is null set empty
  246. if (selector == null) selector = {};
  247. // Build the op
  248. const op = { q: selector, limit: 0 };
  249. if (options.single) {
  250. op.limit = 1;
  251. } else if (finalOptions.retryWrites) {
  252. finalOptions.retryWrites = false;
  253. }
  254. // Have we specified collation
  255. try {
  256. decorateWithCollation(finalOptions, coll, options);
  257. } catch (err) {
  258. return callback(err, null);
  259. }
  260. // Execute the remove
  261. coll.s.topology.remove(coll.s.namespace, [op], finalOptions, (err, result) => {
  262. if (callback == null) return;
  263. if (err) return handleCallback(callback, err, null);
  264. if (result == null) return handleCallback(callback, null, null);
  265. if (result.result.code) return handleCallback(callback, toError(result.result));
  266. if (result.result.writeErrors) {
  267. return handleCallback(callback, toError(result.result.writeErrors[0]));
  268. }
  269. // Return the results
  270. handleCallback(callback, null, result);
  271. });
  272. }
  273. function updateDocuments(coll, selector, document, options, callback) {
  274. if ('function' === typeof options) (callback = options), (options = null);
  275. if (options == null) options = {};
  276. if (!('function' === typeof callback)) callback = null;
  277. // If we are not providing a selector or document throw
  278. if (selector == null || typeof selector !== 'object')
  279. return callback(toError('selector must be a valid JavaScript object'));
  280. if (document == null || typeof document !== 'object')
  281. return callback(toError('document must be a valid JavaScript object'));
  282. // Final options for retryable writes and write concern
  283. let finalOptions = Object.assign({}, options);
  284. finalOptions = applyRetryableWrites(finalOptions, coll.s.db);
  285. finalOptions = applyWriteConcern(finalOptions, { db: coll.s.db, collection: coll }, options);
  286. // Do we return the actual result document
  287. // Either use override on the function, or go back to default on either the collection
  288. // level or db
  289. finalOptions.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
  290. // Execute the operation
  291. const op = { q: selector, u: document };
  292. op.upsert = options.upsert !== void 0 ? !!options.upsert : false;
  293. op.multi = options.multi !== void 0 ? !!options.multi : false;
  294. if (options.hint) {
  295. op.hint = options.hint;
  296. }
  297. if (finalOptions.arrayFilters) {
  298. op.arrayFilters = finalOptions.arrayFilters;
  299. delete finalOptions.arrayFilters;
  300. }
  301. if (finalOptions.retryWrites && op.multi) {
  302. finalOptions.retryWrites = false;
  303. }
  304. // Have we specified collation
  305. try {
  306. decorateWithCollation(finalOptions, coll, options);
  307. } catch (err) {
  308. return callback(err, null);
  309. }
  310. // Update options
  311. coll.s.topology.update(coll.s.namespace, [op], finalOptions, (err, result) => {
  312. if (callback == null) return;
  313. if (err) return handleCallback(callback, err, null);
  314. if (result == null) return handleCallback(callback, null, null);
  315. if (result.result.code) return handleCallback(callback, toError(result.result));
  316. if (result.result.writeErrors)
  317. return handleCallback(callback, toError(result.result.writeErrors[0]));
  318. // Return the results
  319. handleCallback(callback, null, result);
  320. });
  321. }
  322. function updateCallback(err, r, callback) {
  323. if (callback == null) return;
  324. if (err) return callback(err);
  325. if (r == null) return callback(null, { result: { ok: 1 } });
  326. r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
  327. r.upsertedId =
  328. Array.isArray(r.result.upserted) && r.result.upserted.length > 0
  329. ? r.result.upserted[0] // FIXME(major): should be `r.result.upserted[0]._id`
  330. : null;
  331. r.upsertedCount =
  332. Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
  333. r.matchedCount =
  334. Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? 0 : r.result.n;
  335. callback(null, r);
  336. }
  337. module.exports = {
  338. buildCountCommand,
  339. deleteCallback,
  340. findAndModify,
  341. indexInformation,
  342. nextObject,
  343. prepareDocs,
  344. insertDocuments,
  345. removeDocuments,
  346. updateDocuments,
  347. updateCallback
  348. };