2014-04-21 10:32:51 -04:00
|
|
|
// Test $elemMatch object with complex embedded expressions.
|
2024-12-27 16:01:08 +04:00
|
|
|
// @tags: [
|
|
|
|
|
// requires_getmore,
|
|
|
|
|
// ]
|
2014-04-21 10:32:51 -04:00
|
|
|
|
2025-08-28 15:11:44 -04:00
|
|
|
let t = db.jstests_arrayfindb;
|
2014-04-21 10:32:51 -04:00
|
|
|
t.drop();
|
|
|
|
|
|
|
|
|
|
// Case #1: Ensure correct matching for $elemMatch with an embedded $and (SERVER-13664).
|
2025-08-21 10:17:44 -07:00
|
|
|
t.save({
|
|
|
|
|
a: [
|
|
|
|
|
{b: 1, c: 25},
|
|
|
|
|
{a: 3, b: 59},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
assert.eq(
|
|
|
|
|
0,
|
|
|
|
|
t.find({a: {$elemMatch: {b: {$gte: 2, $lt: 4}, c: 25}}}).itcount(),
|
|
|
|
|
"Case #1: wrong number of results returned -- unindexed",
|
|
|
|
|
);
|
2014-04-21 10:32:51 -04:00
|
|
|
|
2020-11-17 13:45:05 +00:00
|
|
|
t.createIndex({"a.b": 1, "a.c": 1});
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.eq(
|
|
|
|
|
0,
|
|
|
|
|
t.find({a: {$elemMatch: {b: {$gte: 2, $lt: 4}, c: 25}}}).itcount(),
|
|
|
|
|
"Case #1: wrong number of results returned -- indexed",
|
|
|
|
|
);
|
2014-04-21 10:32:51 -04:00
|
|
|
|
|
|
|
|
// Case #2: Ensure correct matching for $elemMatch with an embedded $or.
|
|
|
|
|
t.drop();
|
|
|
|
|
t.save({a: [{b: 1}, {c: 1}]});
|
|
|
|
|
t.save({a: [{b: 2}, {c: 1}]});
|
|
|
|
|
t.save({a: [{b: 1}, {c: 2}]});
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.eq(
|
|
|
|
|
2,
|
|
|
|
|
t.find({a: {$elemMatch: {$or: [{b: 2}, {c: 2}]}}}).itcount(),
|
|
|
|
|
"Case #2: wrong number of results returned -- unindexed",
|
|
|
|
|
);
|
2014-04-21 10:32:51 -04:00
|
|
|
|
2020-11-17 13:45:05 +00:00
|
|
|
t.createIndex({"a.b": 1});
|
|
|
|
|
t.createIndex({"a.c": 1});
|
2025-08-21 10:17:44 -07:00
|
|
|
assert.eq(
|
|
|
|
|
2,
|
|
|
|
|
t.find({a: {$elemMatch: {$or: [{b: 2}, {c: 2}]}}}).itcount(),
|
|
|
|
|
"Case #2: wrong number of results returned -- indexed",
|
|
|
|
|
);
|