This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -11,6 +11,7 @@ const MongoError = require('../core').MongoError;
const ReadPreference = require('../core').ReadPreference;
const toError = require('../utils').toError;
const CursorState = require('../core/cursor').CursorState;
const maxWireVersion = require('../core/utils').maxWireVersion;
/**
* Build the count command.
@@ -57,14 +58,6 @@ function buildCountCommand(collectionOrCursor, query, options) {
return cmd;
}
function deleteCallback(err, r, callback) {
if (callback == null) return;
if (err && callback) return callback(err);
if (r == null) return callback(null, { result: { ok: 1 } });
r.deletedCount = r.result.n;
if (callback) callback(null, r);
}
/**
* Find and update a document.
*
@@ -297,6 +290,9 @@ function removeDocuments(coll, selector, options, callback) {
} else if (finalOptions.retryWrites) {
finalOptions.retryWrites = false;
}
if (options.hint) {
op.hint = options.hint;
}
// Have we specified collation
try {
@@ -305,6 +301,12 @@ function removeDocuments(coll, selector, options, callback) {
return callback(err, null);
}
if (options.explain !== undefined && maxWireVersion(coll.s.topology) < 3) {
return callback
? callback(new MongoError(`server does not support explain on remove`))
: undefined;
}
// Execute the remove
coll.s.topology.remove(coll.s.namespace, [op], finalOptions, (err, result) => {
if (callback == null) return;
@@ -366,6 +368,12 @@ function updateDocuments(coll, selector, document, options, callback) {
return callback(err, null);
}
if (options.explain !== undefined && maxWireVersion(coll.s.topology) < 3) {
return callback
? callback(new MongoError(`server does not support explain on update`))
: undefined;
}
// Update options
coll.s.topology.update(coll.s.namespace, [op], finalOptions, (err, result) => {
if (callback == null) return;
@@ -379,31 +387,13 @@ function updateDocuments(coll, selector, document, options, callback) {
});
}
function updateCallback(err, r, callback) {
if (callback == null) return;
if (err) return callback(err);
if (r == null) return callback(null, { result: { ok: 1 } });
r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
r.upsertedId =
Array.isArray(r.result.upserted) && r.result.upserted.length > 0
? r.result.upserted[0] // FIXME(major): should be `r.result.upserted[0]._id`
: null;
r.upsertedCount =
Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
r.matchedCount =
Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? 0 : r.result.n;
callback(null, r);
}
module.exports = {
buildCountCommand,
deleteCallback,
findAndModify,
indexInformation,
nextObject,
prepareDocs,
insertDocuments,
removeDocuments,
updateDocuments,
updateCallback
updateDocuments
};