MongoDB basics

Follow

Querying MongoDB data

This section describes how to construct MongoDB queries based to select needed documents.

Query object is a simple JSON object, that allows you to write expressions to match properties in the documents you want to retrieve.

Matching exact values

To select all documents where some property has specific value you just need to provide property as key and your selected value. Note that type and case must match value exactly as it is stored.

1 property multiple properties type match case match
//selecting documents with did property and value "123"
{"did":"123"}

This also matches values in properties has arrays. Assuming document with property like {"tags":["a","b","c"]}. Selecting documents where tags contain a value in the array would be simply as:

{"tags":"a"}

Querying subproperties

If your value is inside some other object, like {"custom":{"key":"val"}} then you can query it by concatenating key with dot.

{"custom.key":"val"}

None equality

In similar manner you can query the documents which properties does not equal specific values. Same rules apply about type and case checks.

1 property multiple properties mixed
//selecting documents with did property not equal value "123"
{"did":{"$ne":"123"}}

Querying ranges

Instead of querying specific value you can query ranges of different values. For numbers, it expresses as greater than, lesser than operators.

  • $gt - greater than
  • $lt - less than
  • $gte - greater or equal than
  • $lte - less or equal than
//qty property value greater than 20
{"qty":{"$gt":20}}

//qty property value in the ranges between 10 and 20 inclusive
{"qty":{"$gte":10,"$lte":20}}

//qty property value in the ranges between 10 and 20 inclusive
//and uid equal "abc"
{"qty":{"$gte":10,"$lte":20},"uid":"abc"}

Query sets of values

For string values we can define sets that selected documents should or should not include using operators:

  • $in - to include at least one of the values
  • $nin - does not include any of the values
includes excludes
//select documents where property uid equals to "abc" or "cba"
{"uid":{"$in":["abc","cba"]}}

Checking if property exists

To check if property exists or does not exist can be done using $exists operator

exists does not exist
//select all documents which have property data with any type except undefined
{"data":{"$exists":true}}

More information: https://docs.mongodb.com/manual/reference/operator/query/

MongoDB projection

This section describes how to use projection to return only the fields you need.

Projection is a simple JSON object, with field name as key and value as 1 or 0, to indicate, respectively, if field should be returned or not. By default _id field is always returned, and should be explicitly excluded by specifying {_id:0}

Excluding fields Returning fields Returning fields without _id
//these fields will be excluded, all other fields returned
{"did":0,"uid":0,"custom":0} 

More information: https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/

MongoDB updates

This section describes how to construct MongoDB update objects to change data inside database.

To change propertied of the document update object should contain operators to describe what needs to be done with specified field. These operators are:

  • $set - sets or changes the value of a property
  • $unset - removes the specified property from a document
  • $inc - increments the value of the property by the specified amount
  • $mul - multiplies the value of the property by the specified amount
  • $min - only updates the property if the specified value is less than the existing value.
  • $max - only updates the property if the specified value is greater than the existing value.
set unset inc mul min max mixed
//change value of data to "123"
{"$set":{"data":"123"}}

//change value of data to "123" and uid to "abc"
{"$set":{"data":"123","uid":"abc"}}

//change value of sub property old in {"data":{"old":true}} to false 
{"$set":{"data.old":false}}

More information: https://docs.mongodb.com/manual/reference/operator/update/

Looking for help?