MongoDB Query Structure
MongoDB queries are based on JSON documents in which you write your criteria in the form of valid documents. With the data stored in the form of JSON-like documents, the queries seem more natural and readable. The following diagram is an example of a simple MongoDB query that finds all the documents where the name
field contains the value David
:
To draw a comparison with SQL, let's rewrite the same query in SQL format. This query finds all the rows from the USERS
table that contain the name
column where the value of name
is David
, as follows:
SELECT * FROM USERS WHERE name = 'David';
The most notable difference between the preceding queries is that the MongoDB queries do not have keywords such as SELECT
, FROM
, and WHERE
. Thus, you need not remember a lot of keywords and their uses.
The absence of keywords makes the queries less wordy and hence more focused, and less error-prone...