MongoDB — Querying Arrays & Multikey Fields
Learn array matching rules, $all, $elemMatch, and array length ($size) query selectors.
1Array Element Equality Matching
When you query an array field with a scalar value, MongoDB automatically matches if that value exists ANYWHERE in the array:
Array Equality Query
// Matches if "MongoDB" is present inside the skills array
db.users.find({ skills: "MongoDB" });
2Using $all and $elemMatch
$all & $elemMatch Examples
// $all: Must contain BOTH "MongoDB" AND "Node.js" in array
db.users.find({ skills: { $all: ["MongoDB", "Node.js"] } });
// $elemMatch: Matches array elements satisfying MULTIPLE conditions
db.scores.find({
results: { $elemMatch: { score: { $gte: 80 }, subject: "Math" } }
});