You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
864 B
38 lines
864 B
5 years ago
|
'use strict';
|
||
|
|
||
|
const handleCallback = require('../utils').handleCallback;
|
||
|
const OperationBase = require('./operation').OperationBase;
|
||
|
const toError = require('../utils').toError;
|
||
|
|
||
|
class FindOneOperation extends OperationBase {
|
||
|
constructor(collection, query, options) {
|
||
|
super(options);
|
||
|
|
||
|
this.collection = collection;
|
||
|
this.query = query;
|
||
|
}
|
||
|
|
||
|
execute(callback) {
|
||
|
const coll = this.collection;
|
||
|
const query = this.query;
|
||
|
const options = this.options;
|
||
|
|
||
|
try {
|
||
|
const cursor = coll
|
||
|
.find(query, options)
|
||
|
.limit(-1)
|
||
|
.batchSize(1);
|
||
|
|
||
|
// Return the item
|
||
|
cursor.next((err, item) => {
|
||
|
if (err != null) return handleCallback(callback, toError(err), null);
|
||
|
handleCallback(callback, null, item);
|
||
|
});
|
||
|
} catch (e) {
|
||
|
callback(e);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = FindOneOperation;
|