Skip to content

Commit

Permalink
add: test for nested in
Browse files Browse the repository at this point in the history
  • Loading branch information
tosaka-n committed Dec 14, 2023
1 parent eaf1425 commit 130dcec
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/model.query.casting.test.js
Expand Up @@ -791,6 +791,38 @@ describe('model query casting', function() {
assert.ok(res);
assert.deepStrictEqual(res.map(doc => doc.arr[1].id), ['two', 'three']);
});

it('should not throw a cast error when dealing with an array of objects in combination with $elemMatch and nested $and', async function() {
const testSchema = new Schema({
arr: [Object]
});

const Test = db.model('Test', testSchema);
const obj1 = new Test({ arr: [{ id: 'one', name: 'sample1' }, { id: 'two' }] });
await obj1.save();

const obj2 = new Test({ arr: [{ id: 'two', name: 'sample1' }, { id: 'three' }] });
await obj2.save();

const obj3 = new Test({ arr: [{ id: 'three', name: 'sample1' }, { id: 'four' }] });
await obj3.save();
const res = await Test.find({
arr: {
$elemMatch: {
$and: [
{ name: 'sample1' },
{ $or: [
{ id: 'one' },
{ id: 'two' }
] }
]
}
}
}).sort({ _id: 1 });
assert.ok(res);
assert.equal(res.length, 2);
assert.deepStrictEqual(res.map(doc => doc.arr[1].id), ['two', 'three']);
});
});

function _geojsonPoint(coordinates) {
Expand Down

0 comments on commit 130dcec

Please sign in to comment.