Skip to content

Commit

Permalink
Fix $map transform, add tests for $map and $explain
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyhwang committed Feb 13, 2024
1 parent 8270f4a commit b54863a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,7 @@ var cursorOperationsMap = {
},
$map: function(cursor, fn, cb) {
cursor.map(fn)
.toArray()
.then(function(result) {
cb(null, result);
}, cb);
Expand Down
54 changes: 54 additions & 0 deletions test/test_mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,60 @@ describe('mongo db', function() {
});
});

it('$map maps docs with output in extra', function(done) {
var snapshots = [
{type: 'json0', id: 'test1', v: 1, data: {x: 1, y: 1}},
{type: 'json0', id: 'test2', v: 1, data: {x: 2, y: 2}},
{type: 'json0', id: 'test3', v: 1, data: {x: 3, y: 2}}
];
var query = {
y: 2,
$map: function(doc) {
return doc.x;
}
};

var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results, extra) {
if (err) return done(err);

// Since $map can return non-docs, the output is delivered in extra.
expect(results).eql([]);
expect(extra).to.deep.equal([2, 3]);
done();
});
});
});

it('$explain delivers output in extra', function(done) {
var snapshots = [
{type: 'json0', id: 'test1', v: 1, data: {x: 1, y: 1}},
{type: 'json0', id: 'test2', v: 1, data: {x: 2, y: 2}},
{type: 'json0', id: 'test3', v: 1, data: {x: 3, y: 2}}
];
var query = {$explain: true, y: 2};

var db = this.db;
async.each(snapshots, function(snapshot, cb) {
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, cb);
}, function(err) {
if (err) return done(err);
db.query('testcollection', query, null, null, function(err, results, extra) {
if (err) return done(err);

expect(results).eql([]);
// Just check for the presence of an explain result. The specific structure
// could vary between Mongo versions.
expect(extra).to.be.an('object');
done();
});
});
});

it('$sort, $skip and $limit should order, skip and limit', function(done) {
var snapshots = [
{type: 'json0', v: 1, data: {x: 1}, id: 'test1', m: null},
Expand Down

0 comments on commit b54863a

Please sign in to comment.