Updated March 8, 2023
Definition of MongoDB Query Array
MongoDB query array operator is used to query documents with an array, we can retrieve array element of data by using query array operator in MongoDB. There are three types of query array operators available in MongoDB, we need to use prefix as $ sign before using query array operator. We can also perform multiple operations on the array field in MongoDB, we can use the insert method for inserting the array element field into the collection. Also, we can retrieve all array fields or specific fields from the collection in MongoDB.
Syntax:
Below syntax shows query array in MongoDB.
1) MongoDB query array using operator –
db.name_of_collection.find ( { “array_field_name”: { $array_query_operator_name: [ Array_element_value1, …, Array_element_valueN] } } ) .pretty()
2) MongoDB query array to insert array documents into the collection –
db.name_of_collection.insert ( { array_field_name : [ value1, value2, …., valueN] } )
3) MongoDB query array to retrieve specified elements from the array –
db.name_of_collection.find ( { array_field_name : [value] } )
4) MongoDB query array to retrieve specified elements using condition –
db.name_of_collection.find ( { array_field_name : { operator_name: [“value”], operator_name: [“value”] } } )
Parameter:
1) Name of collection – This parameter is defined as the collection name from which we have retrieving array documents as per the query operators which was we have used in our query.
2) Find – This method is used to retrieve array elements from collections. We can also use the find method with an array query operator to retrieve specific array elements in MongoDB.
3) Array field name – This parameter is defined as the name of the array field from which we have retrieving elements as per the query parameter which was we have used in our query.
4) Query operator name – This parameter is defined as the name of the array operator which was we have used in our query.
5) Value1 to valueN – The value nothing but the array field element value which was we have using with array query operator to retrieve the documents from the collection
6) Pretty – Basically MongoDB output is in an unstructured format using this method we can display array element documents output in a structured format.
How query array works in MongoDB?
- We can insert the array element field in MongoDB by using the insert method. We can also insert multiple array files using insertMany method.
- We can query an array element by using the equality condition to exact match of array element field.
- There are three types of query array operators available in MongoDB are as follows.
1) $all – It will match all the elements from an array that satisfy the given condition.
2) $size – It will match the array as per specified size is equal to the array size.
3) $elemMatch – It will match the documents as per the specified elemMatch condition.
- Array operators in MongoDB are basically designed for arrays with query documents.
- Using all array query operators we can find all the array field elements which were we have specified in our query.
- Using the size operator we can find all the array documents which will match our specified array size is the defined query.
- To find specified array elements from the array field we need to specify the specified array elements in our query.
- We can use MongoDB array query operators in any array of documents that was supported by MongoDB.
- We can also query an array by specifying the compound condition on array elements, we can specify our query either by using a single element or multiple elements which meet the condition.
- To use the array query operator in our query we need to specify the $ sign as a prefix. At the time of using any operator in MongoDB prefix, $ sign is mandatory to use query operator in query.
- I suppose we have not used $ sign it will show syntax error.
Example
The below example shows the query array in MongoDB.
1) MongoDB query array to insert array document into the collection
- The below example shows that MongoDB query array to insert array document into the query_array collection.
- We have used an insert and insertMany methods to insert documents into the collection.
- In the first example, we have inserted single documents by using the insert method and in the second example we have inserted three documents by using insertMany method.
Code:
use MongoDB
db.query_array.insertMany ( [ { stud_id: 1, stud_name: "ABC", grades: ["A", "B", "C", "D"], marks: [ 50, 60, 70, 80, 90 ] } ] );
db.query_array.insertMany ( [
{ stud_id: 1, stud_name: "ABC", grades: ["A", "B", "C", "D"], marks: [ 50, 60, 70, 80, 90 ] },
{ stud_id: 2, stud_name: "PQR", grades: ["A", "B", "C", "D"], marks: [ 55, 65, 75, 85, 95 ] },
{ stud_id: 3, stud_name: "XYZ", grades: ["A", "B", "C", "D"], marks: [ 50, 60, 70, 80, 90 ] }
]);
db.query_array.find ()
Figure – Example of MongoDB query array to insert array document into the collection.
2) MongoDB query array by using array operators
- The below example shows that MongoDB query array by using array operators.
- We have used the $all and $size operators to retrieve array elements from query_array collection.
- In the first example, we have used the $all operator to retrieve data using the marks array field. In the second example we have used the $size operator and define size as 4 on the grades field.
Code:
db.query_array.find ( { "marks": { $all: [ 50, 70 ] } } )
db.query_array.find ( { "grades": { $size: 4 } } )
db.query_array.find ()
Figure – Example of MongoDB query array by using array operators.
3) MongoDB query array by using specified array field
The below example shows the MongoDB query array by using a specified array field. We have used array field names as marks and define elements as 50, 60, 70, 80, and 80.
Code:
db.query_array.find ( { marks: [50, 60, 70, 80, 90] } )
db.query_array.find ()
Figure – Example of MongoDB query array by using specified array field.
4) MongoDB query array by using elemMatch and comparison operator
- The below example shows the MongoDB query array by using elemMatch and comparison operator.
- We have used the $gt and $lt comparison operators to retrieve documents from the array field.
Code:
db.query_array.find ( { "marks": { $elemMatch: {$gt: 50, $lt: 95 } } } )
db.query_array.find ()
Figure – Example of MongoDB query array by using elemMatch and comparison operator.
Conclusion
MongoDB query array is defined as an insert or retrieves array documents or elements from the collection. We can retrieve specified elements or documents by defining array elements into the query. We can use multiple query operators to retrieve data for array elements from the collection in MongoDB.
Recommended Articles
This is a guide to MongoDB Query Array. Here we discuss the definition, syntax, How query array works in MongoDB? examples with code implementation. You may also have a look at the following articles to learn more –