MongoDB $elemMatch

Summary: in this tutorial, you’ll learn how to use the MongoDB $elemMatch operator to select documents from a collection.

Introduction to the MongoDB $elemMatch operator

The $elemMatch is an array query operator that matches documents:

  • which contains an array field.
  • and the array field has at least one element that satisfies all the specified queries.

The $elemMatch has the following syntax:

{ <arrayField>: {$elemMatch: { <query1>, <query2>, ...} } }
Code language: HTML, XML (xml)

In this syntax:

  • First, specify the name of the array field.
  • Second, specify a list of queries that you want at least one element in the <arrayField> to meet the query criteria.

Notice that you cannot specify a $where expression or a $text query expression in an $elemMatch.

MongoDB $elemMatch operator examples

We’ll use the following products collection:

db.products.insertMany([ { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate" : ISODate("2011-05-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 }, "color" : [ "white", "black" ], "storage" : [ 64, 128, 256 ] }, { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate" : ISODate("2011-09-01T00:00:00Z"), "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 }, "color" : [ "white", "black", "purple" ], "storage" : [ 128, 256, 512 ] }, { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate" : ISODate("2015-01-14T00:00:00Z"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 }, "color" : [ "blue" ], "storage" : [ 16, 64, 128 ] }, { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate" : ISODate("2020-05-14T00:00:00Z"), "spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 1024 ] }, { "_id" : 5, "name" : "SmartPhone", "price" : 599, "releaseDate" : ISODate("2022-09-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256 ] } ])
Code language: JavaScript (javascript)

1) Using the MongDB $elemMatch opeator example

The following example uses the $elemMatch operator to query documents from the products collection. It matches the documents where the storage is an array that contains at least one element greater than or equal to 64 and less than or equal to 128:

db.products.find({ storage: { $gte: 64, $lte: 128 } }, { name: 1, storage: 1 });
Code language: CSS (css)

It returned the following documents:

{ "_id" : 1, "name" : "xPhone", "storage" : [ 64, 128, 256 ] } { "_id" : 2, "name" : "xTablet", "storage" : [ 128, 256, 512 ] } { "_id" : 4, "name" : "SmartPad", "storage" : [ 128, 256, 1024 ] } { "_id" : 5, "name" : "SmartPhone", "storage" : [ 128, 256 ] }
Code language: JSON / JSON with Comments (json)

Summary

  • Use the $elemMatch operator to select documents that have an array field. And the array field has at least one element that satisfies specified query criteria.


Was this tutorial helpful ?