Which operator is used to exclude specific values from a query in MongoDB?

Prepare for the MongoDB Associate Developer Exam with flashcards and practice multiple choice questions. Each question includes hints and explanations to ensure you're ready for test day.

The operator used to exclude specific values from a query in MongoDB is $nin. This operator stands for "not in" and allows you to filter documents by specifying an array of values that should not be present in a given field. When you use $nin in a query, MongoDB will return documents where the value of the specified field does not match any of the values in the array provided.

For instance, if you are querying a collection of products and want to find all products that are not in a certain category, you could use $nin with an array containing the categories you want to exclude. This makes $nin particularly useful for filtering out unwanted results based on specified criteria.

In contrast, the other operators listed serve different purposes. $in is used to include documents with values that match any values in a specified array. $nor is a logical operator that can be used to combine multiple conditions and return documents that do not meet any of the specified conditions. $not, on the other hand, is utilized to apply negation to a query condition but does not specifically handle arrays like $nin does. Therefore, for the purpose of excluding specific values from a query, $nin is the appropriate operator to use.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy