@@ -12,7 +12,9 @@ import {
12
12
checkCollectionName ,
13
13
deprecateOptions ,
14
14
executeLegacyOperation ,
15
- MongoDBNamespace
15
+ MongoDBNamespace ,
16
+ handleCallback ,
17
+ applyWriteConcern
16
18
} from './utils' ;
17
19
import { ObjectId } from './bson' ;
18
20
import { MongoError } from './error' ;
@@ -22,32 +24,37 @@ import ChangeStream = require('./change_stream');
22
24
import WriteConcern = require( './write_concern' ) ;
23
25
import ReadConcern = require( './read_concern' ) ;
24
26
import { AggregationCursor , CommandCursor } from './cursor' ;
25
- import { ensureIndex , group , save , checkForAtomicOperators } from './operations/collection_ops' ;
26
- import { removeDocuments , updateDocuments } from './operations/common_functions' ;
27
+ import { removeDocuments , updateDocuments , insertDocuments } from './operations/common_functions' ;
27
28
import AggregateOperation = require( './operations/aggregate' ) ;
28
29
import BulkWriteOperation = require( './operations/bulk_write' ) ;
29
30
import CountDocumentsOperation = require( './operations/count_documents' ) ;
30
- import CreateIndexesOperation = require( './operations/create_indexes' ) ;
31
+ import {
32
+ CreateIndexesOperation ,
33
+ CreateIndexOperation ,
34
+ DropIndexOperation ,
35
+ DropIndexesOperation ,
36
+ EnsureIndexOperation ,
37
+ IndexesOperation ,
38
+ IndexExistsOperation ,
39
+ IndexInformationOperation ,
40
+ ListIndexesOperation
41
+ } from './operations/indexes' ;
31
42
import DeleteManyOperation = require( './operations/delete_many' ) ;
32
43
import DeleteOneOperation = require( './operations/delete_one' ) ;
33
44
import DistinctOperation = require( './operations/distinct' ) ;
34
45
import { DropCollectionOperation } from './operations/drop' ;
35
- import DropIndexOperation = require( './operations/drop_index' ) ;
36
- import DropIndexesOperation = require( './operations/drop_indexes' ) ;
37
46
import EstimatedDocumentCountOperation = require( './operations/estimated_document_count' ) ;
38
47
import FindOperation = require( './operations/find' ) ;
39
48
import FindOneOperation = require( './operations/find_one' ) ;
40
- import FindAndModifyOperation = require( './operations/find_and_modify' ) ;
41
- import FindOneAndDeleteOperation = require( './operations/find_one_and_delete' ) ;
42
- import FindOneAndReplaceOperation = require( './operations/find_one_and_replace' ) ;
43
- import FindOneAndUpdateOperation = require( './operations/find_one_and_update' ) ;
44
- import IndexesOperation = require( './operations/indexes' ) ;
45
- import IndexExistsOperation = require( './operations/index_exists' ) ;
46
- import IndexInformationOperation = require( './operations/index_information' ) ;
49
+ import {
50
+ FindAndModifyOperation ,
51
+ FindOneAndDeleteOperation ,
52
+ FindOneAndReplaceOperation ,
53
+ FindOneAndUpdateOperation
54
+ } from './operations/find_and_modify' ;
47
55
import InsertManyOperation = require( './operations/insert_many' ) ;
48
56
import InsertOneOperation = require( './operations/insert_one' ) ;
49
57
import IsCappedOperation = require( './operations/is_capped' ) ;
50
- import ListIndexesOperation = require( './operations/list_indexes' ) ;
51
58
import MapReduceOperation = require( './operations/map_reduce' ) ;
52
59
import OptionsOperation = require( './operations/options_operation' ) ;
53
60
import RenameOperation = require( './operations/rename' ) ;
@@ -56,6 +63,7 @@ import { CollStatsOperation } from './operations/stats';
56
63
import UpdateManyOperation = require( './operations/update_many' ) ;
57
64
import UpdateOneOperation = require( './operations/update_one' ) ;
58
65
import executeOperation = require( './operations/execute_operation' ) ;
66
+ import { EvalGroupOperation , GroupOperation } from './operations/group' ;
59
67
const mergeKeys = [ 'ignoreUndefined' ] ;
60
68
61
69
interface Collection {
@@ -199,7 +207,7 @@ class Collection {
199
207
* @memberof Collection#
200
208
* @readonly
201
209
*/
202
- get dbName ( ) {
210
+ get dbName ( ) : string {
203
211
return this . s . namespace . db ;
204
212
}
205
213
@@ -210,7 +218,7 @@ class Collection {
210
218
* @memberof Collection#
211
219
* @readonly
212
220
*/
213
- get collectionName ( ) {
221
+ get collectionName ( ) : string {
214
222
return this . s . namespace . collection ;
215
223
}
216
224
@@ -798,7 +806,7 @@ class Collection {
798
806
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
799
807
options = options || { } ;
800
808
801
- const createIndexesOperation = new CreateIndexesOperation (
809
+ const createIndexesOperation = new CreateIndexOperation (
802
810
this ,
803
811
this . collectionName ,
804
812
fieldOrSpec ,
@@ -1971,12 +1979,11 @@ Collection.prototype.ensureIndex = deprecate(function(
1971
1979
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
1972
1980
options = options || { } ;
1973
1981
1974
- return executeLegacyOperation ( this . s . topology , ensureIndex , [
1975
- this ,
1976
- fieldOrSpec ,
1977
- options ,
1982
+ return executeOperation (
1983
+ this . s . topology ,
1984
+ new EnsureIndexOperation ( this . s . db , this . collectionName , fieldOrSpec , options ) ,
1978
1985
callback
1979
- ] ) ;
1986
+ ) ;
1980
1987
} ,
1981
1988
'collection.ensureIndex is deprecated. Use createIndexes instead.' ) ;
1982
1989
@@ -2185,18 +2192,71 @@ Collection.prototype.group = deprecate(function(
2185
2192
// Set up the command as default
2186
2193
command = command == null ? true : command ;
2187
2194
2188
- return executeLegacyOperation ( this . s . topology , group , [
2189
- this ,
2190
- keys ,
2191
- condition ,
2192
- initial ,
2193
- reduce ,
2194
- finalize ,
2195
- command ,
2196
- options ,
2195
+ if ( command == null ) {
2196
+ return executeOperation (
2197
+ this . s . topology ,
2198
+ new EvalGroupOperation ( this , keys , condition , initial , reduce , finalize , options ) ,
2199
+ callback
2200
+ ) ;
2201
+ }
2202
+
2203
+ return executeOperation (
2204
+ this . s . topology ,
2205
+ new GroupOperation ( this , keys , condition , initial , reduce , finalize , options ) ,
2197
2206
callback
2198
- ] ) ;
2207
+ ) ;
2199
2208
} ,
2200
2209
'MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.' ) ;
2201
2210
2211
+
2212
+ // Check the update operation to ensure it has atomic operators.
2213
+ function checkForAtomicOperators ( update : any ) : any {
2214
+ if ( Array . isArray ( update ) ) {
2215
+ return update . reduce ( ( err ?: any , u ?: any ) => err || checkForAtomicOperators ( u ) , null ) ;
2216
+ }
2217
+
2218
+ const keys = Object . keys ( update ) ;
2219
+
2220
+ // same errors as the server would give for update doc lacking atomic operators
2221
+ if ( keys . length === 0 ) {
2222
+ return toError ( 'The update operation document must contain at least one atomic operator.' ) ;
2223
+ }
2224
+
2225
+ if ( keys [ 0 ] [ 0 ] !== '$' ) {
2226
+ return toError ( 'the update operation document must contain atomic operators.' ) ;
2227
+ }
2228
+ }
2229
+
2230
+ /**
2231
+ * Save a document.
2232
+ *
2233
+ * @function
2234
+ * @param {Collection } coll Collection instance.
2235
+ * @param {any } doc Document to save
2236
+ * @param {any } [options] Optional settings. See Collection.prototype.save for a list of options.
2237
+ * @param {Collection~writeOpCallback } [callback] The command result callback
2238
+ * @deprecated use insertOne, insertMany, updateOne or updateMany
2239
+ */
2240
+ function save ( coll : any , doc : any , options ?: any , callback ?: Function ) {
2241
+ // Get the write concern options
2242
+ const finalOptions = applyWriteConcern (
2243
+ Object . assign ( { } , options ) ,
2244
+ { db : coll . s . db , collection : coll } ,
2245
+ options
2246
+ ) ;
2247
+ // Establish if we need to perform an insert or update
2248
+ if ( doc . _id != null ) {
2249
+ finalOptions . upsert = true ;
2250
+ return updateDocuments ( coll , { _id : doc . _id } , doc , finalOptions , callback ) ;
2251
+ }
2252
+
2253
+ // Insert the document
2254
+ insertDocuments ( coll , [ doc ] , finalOptions , ( err ?: any , result ?: any ) => {
2255
+ if ( callback == null ) return ;
2256
+ if ( doc == null ) return handleCallback ( callback , null , null ) ;
2257
+ if ( err ) return handleCallback ( callback , err , null ) ;
2258
+ handleCallback ( callback , null , result ) ;
2259
+ } ) ;
2260
+ }
2261
+
2202
2262
export = Collection ;
0 commit comments