find_one.js
864 Bytes
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
'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;