Skip to content

Commit b052e65

Browse files
committed
add: test for nested $and in $elemMatch
1 parent eaf1425 commit b052e65

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

test/model.query.casting.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,38 @@ describe('model query casting', function() {
791791
assert.ok(res);
792792
assert.deepStrictEqual(res.map(doc => doc.arr[1].id), ['two', 'three']);
793793
});
794+
795+
it('should not throw a cast error when dealing with an array of objects in combination with $elemMatch and nested $and', async function() {
796+
const testSchema = new Schema({
797+
arr: [Object]
798+
});
799+
800+
const Test = db.model('Test', testSchema);
801+
const obj1 = new Test({ arr: [{ id: 'one', name: 'sample1' }, { id: 'two' }] });
802+
await obj1.save();
803+
804+
const obj2 = new Test({ arr: [{ id: 'two', name: 'sample1' }, { id: 'three' }] });
805+
await obj2.save();
806+
807+
const obj3 = new Test({ arr: [{ id: 'three', name: 'sample1' }, { id: 'four' }] });
808+
await obj3.save();
809+
const res = await Test.find({
810+
arr: {
811+
$elemMatch: {
812+
$and: [
813+
{ name: 'sample1' },
814+
{ $or: [
815+
{ id: 'one' },
816+
{ id: 'two' }
817+
] }
818+
]
819+
}
820+
}
821+
}).sort({ _id: 1 });
822+
assert.ok(res);
823+
assert.equal(res.length, 2);
824+
assert.deepStrictEqual(res.map(doc => doc.arr[1].id), ['two', 'three']);
825+
});
794826
});
795827

796828
function _geojsonPoint(coordinates) {

0 commit comments

Comments
 (0)