find_and_modify.js
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const OperationBase = require('./operation').OperationBase;
const applyRetryableWrites = require('../utils').applyRetryableWrites;
const applyWriteConcern = require('../utils').applyWriteConcern;
const decorateWithCollation = require('../utils').decorateWithCollation;
const executeCommand = require('./db_ops').executeCommand;
const formattedOrderClause = require('../utils').formattedOrderClause;
const handleCallback = require('../utils').handleCallback;
const ReadPreference = require('../core').ReadPreference;
const maxWireVersion = require('../core/utils').maxWireVersion;
const MongoError = require('../error').MongoError;
const Aspect = require('./operation').Aspect;
const defineAspects = require('./operation').defineAspects;
const decorateWithExplain = require('../utils').decorateWithExplain;
class FindAndModifyOperation extends OperationBase {
constructor(collection, query, sort, doc, options) {
super(options);
this.collection = collection;
this.query = query;
this.sort = sort;
this.doc = doc;
}
execute(callback) {
const coll = this.collection;
const query = this.query;
const sort = formattedOrderClause(this.sort);
const doc = this.doc;
let options = this.options;
// Create findAndModify command object
let queryObject = {
findAndModify: coll.collectionName,
query: query
};
if (sort) {
queryObject.sort = sort;
}
queryObject.new = options.new ? true : false;
queryObject.remove = options.remove ? true : false;
queryObject.upsert = options.upsert ? true : false;
const projection = options.projection || options.fields;
if (projection) {
queryObject.fields = projection;
}
if (options.arrayFilters) {
queryObject.arrayFilters = options.arrayFilters;
}
if (doc && !options.remove) {
queryObject.update = doc;
}
if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;
// Either use override on the function, or go back to default on either the collection
// level or db
options.serializeFunctions = options.serializeFunctions || coll.s.serializeFunctions;
// No check on the documents
options.checkKeys = false;
// Final options for retryable writes and write concern
options = applyRetryableWrites(options, coll.s.db);
options = applyWriteConcern(options, { db: coll.s.db, collection: coll }, options);
// Decorate the findAndModify command with the write Concern
if (options.writeConcern) {
queryObject.writeConcern = options.writeConcern;
}
// Have we specified bypassDocumentValidation
if (options.bypassDocumentValidation === true) {
queryObject.bypassDocumentValidation = options.bypassDocumentValidation;
}
options.readPreference = ReadPreference.primary;
// Have we specified collation
try {
decorateWithCollation(queryObject, coll, options);
} catch (err) {
return callback(err, null);
}
if (options.hint) {
// TODO: once this method becomes a CommandOperationV2 we will have the server
// in place to check.
const unacknowledgedWrite = options.writeConcern && options.writeConcern.w === 0;
if (unacknowledgedWrite || maxWireVersion(coll.s.topology) < 8) {
callback(
new MongoError('The current topology does not support a hint on findAndModify commands')
);
return;
}
queryObject.hint = options.hint;
}
if (this.explain) {
if (maxWireVersion(coll.s.topology) < 4) {
callback(new MongoError(`server does not support explain on findAndModify`));
return;
}
queryObject = decorateWithExplain(queryObject, this.explain);
}
// Execute the command
executeCommand(coll.s.db, queryObject, options, (err, result) => {
if (err) return handleCallback(callback, err, null);
return handleCallback(callback, null, result);
});
}
}
defineAspects(FindAndModifyOperation, [Aspect.EXPLAINABLE]);
module.exports = FindAndModifyOperation;