node.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. /**
  3. * Module dependencies
  4. */
  5. var Collection = require('./collection');
  6. var utils = require('../utils');
  7. function NodeCollection(col) {
  8. this.collection = col;
  9. this.collectionName = col.collectionName;
  10. }
  11. /**
  12. * inherit from collection base class
  13. */
  14. utils.inherits(NodeCollection, Collection);
  15. /**
  16. * find(match, options, function(err, docs))
  17. */
  18. NodeCollection.prototype.find = function(match, options, cb) {
  19. this.collection.find(match, options, function(err, cursor) {
  20. if (err) return cb(err);
  21. try {
  22. cursor.toArray(cb);
  23. } catch (error) {
  24. cb(error);
  25. }
  26. });
  27. };
  28. /**
  29. * findOne(match, options, function(err, doc))
  30. */
  31. NodeCollection.prototype.findOne = function(match, options, cb) {
  32. this.collection.findOne(match, options, cb);
  33. };
  34. /**
  35. * count(match, options, function(err, count))
  36. */
  37. NodeCollection.prototype.count = function(match, options, cb) {
  38. this.collection.count(match, options, cb);
  39. };
  40. /**
  41. * distinct(prop, match, options, function(err, count))
  42. */
  43. NodeCollection.prototype.distinct = function(prop, match, options, cb) {
  44. this.collection.distinct(prop, match, options, cb);
  45. };
  46. /**
  47. * update(match, update, options, function(err[, result]))
  48. */
  49. NodeCollection.prototype.update = function(match, update, options, cb) {
  50. this.collection.update(match, update, options, cb);
  51. };
  52. /**
  53. * update(match, update, options, function(err[, result]))
  54. */
  55. NodeCollection.prototype.updateMany = function(match, update, options, cb) {
  56. this.collection.updateMany(match, update, options, cb);
  57. };
  58. /**
  59. * update(match, update, options, function(err[, result]))
  60. */
  61. NodeCollection.prototype.updateOne = function(match, update, options, cb) {
  62. this.collection.updateOne(match, update, options, cb);
  63. };
  64. /**
  65. * replaceOne(match, update, options, function(err[, result]))
  66. */
  67. NodeCollection.prototype.replaceOne = function(match, update, options, cb) {
  68. this.collection.replaceOne(match, update, options, cb);
  69. };
  70. /**
  71. * deleteOne(match, options, function(err[, result])
  72. */
  73. NodeCollection.prototype.deleteOne = function(match, options, cb) {
  74. this.collection.deleteOne(match, options, cb);
  75. };
  76. /**
  77. * deleteMany(match, options, function(err[, result])
  78. */
  79. NodeCollection.prototype.deleteMany = function(match, options, cb) {
  80. this.collection.deleteMany(match, options, cb);
  81. };
  82. /**
  83. * remove(match, options, function(err[, result])
  84. */
  85. NodeCollection.prototype.remove = function(match, options, cb) {
  86. this.collection.remove(match, options, cb);
  87. };
  88. /**
  89. * findAndModify(match, update, options, function(err, doc))
  90. */
  91. NodeCollection.prototype.findAndModify = function(match, update, options, cb) {
  92. var sort = Array.isArray(options.sort) ? options.sort : [];
  93. this.collection.findAndModify(match, sort, update, options, cb);
  94. };
  95. /**
  96. * var stream = findStream(match, findOptions, streamOptions)
  97. */
  98. NodeCollection.prototype.findStream = function(match, findOptions, streamOptions) {
  99. return this.collection.find(match, findOptions).stream(streamOptions);
  100. };
  101. /**
  102. * var cursor = findCursor(match, findOptions)
  103. */
  104. NodeCollection.prototype.findCursor = function(match, findOptions) {
  105. return this.collection.find(match, findOptions);
  106. };
  107. /**
  108. * aggregation(operators..., function(err, doc))
  109. * TODO
  110. */
  111. /**
  112. * Expose
  113. */
  114. module.exports = exports = NodeCollection;