Skip to content

Commit e70bc4f

Browse files
committed
fix: include using parent fields projection
Signed-off-by: Matteo Cassano <m.cassano@nsi.it>
1 parent 06b7239 commit e70bc4f

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

lib/mongodb.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,29 +1386,31 @@ MongoDB.prototype.all = function all(modelName, filter, options, callback) {
13861386
if (filter.where) {
13871387
query = self.buildWhere(modelName, filter.where, options);
13881388
}
1389-
// Use Object.assign to avoid change filter.fields
1390-
// which will cause error when create model from data
1391-
let fields = undefined;
1392-
if (typeof filter.fields !== 'undefined') {
1393-
fields = [];
1394-
Object.assign(fields, filter.fields);
1395-
}
1396-
1397-
// Convert custom column names
1398-
fields = self.fromPropertyToDatabaseNames(modelName, fields);
13991389

14001390
options = buildOptions({}, options);
14011391

1402-
if (fields) {
1403-
options.projection = fieldsArrayToObj(fields);
1404-
}
14051392
this.execute(modelName, 'find', query, options, processResponse);
14061393

14071394
function processResponse(err, cursor) {
14081395
if (err) {
14091396
return callback(err);
14101397
}
14111398

1399+
// Use Object.assign to avoid change filter.fields
1400+
// which will cause error when create model from data
1401+
let fields = undefined;
1402+
if (typeof filter.fields !== 'undefined') {
1403+
fields = [];
1404+
Object.assign(fields, filter.fields);
1405+
}
1406+
1407+
// Convert custom column names
1408+
fields = self.fromPropertyToDatabaseNames(modelName, fields);
1409+
1410+
if (fields) {
1411+
cursor.project(fieldsArrayToObj(fields));
1412+
}
1413+
14121414
const collation = options && options.collation;
14131415
if (collation) {
14141416
cursor.collation(collation);

test/mongodb.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,36 @@ describe('mongodb connector', function() {
23792379
});
23802380
});
23812381

2382+
it('all return should honor filter.fields and not apply them to included relations', function(done) {
2383+
const user = new User({name: 'Matt'});
2384+
user.save(function(err, user) {
2385+
const post = new Post({title: 'b', content: 'BBB', userId: user.id});
2386+
post.save(function(err, post) {
2387+
db.connector.all(
2388+
'Post',
2389+
{fields: ['title', 'userId'], where: {title: 'b'}, include: 'user'},
2390+
{},
2391+
function(err, posts) {
2392+
should.not.exist(err);
2393+
posts.should.have.lengthOf(1);
2394+
post = posts[0];
2395+
post.should.have.property('title', 'b');
2396+
should.not.exist(post.content);
2397+
should.not.exist(post._id);
2398+
should.not.exist(post.id);
2399+
post.should.have.property('userId');
2400+
post.should.have.property('user');
2401+
const {user} = post;
2402+
user.should.have.property('id');
2403+
user.should.have.property('name', 'Matt');
2404+
2405+
done();
2406+
},
2407+
);
2408+
});
2409+
});
2410+
});
2411+
23822412
it('create should convert id from ObjectID to string', function(done) {
23832413
const oid = new db.ObjectID();
23842414
const sid = oid.toString();

0 commit comments

Comments
 (0)