orders with documents containing a status field, which query returns all orders where the status is NOT 'shipped', 'delivered', or 'returned'?db.orders.find({ status: { $nin: ['shipped', 'delivered', 'returned'] } })The $nin operator selects documents where the field's value is not in the specified array. Options B and D select documents where status is in or not equal to the entire array (which is invalid). Option C uses $not incorrectly.
$nin on a field that does not exist in some documents?When a field is missing, MongoDB treats it as if the field's value is null or undefined. Since null is not in the $nin array, those documents match the query.
$nin?Option B is invalid because $nin expects an array as its value, but here multiple values are passed without array brackets, causing a syntax error.
category field. Which query is more efficient to find documents where category is NOT 'A', 'B', or 'C'?Option A uses $nin directly, which can use the index efficiently. Option A uses multiple $ne conditions on the same field, which MongoDB cannot optimize well. Option A uses $not with $in, which is less efficient. Option A adds an unnecessary $exists check.
db.users.find({ roles: { $nin: ['admin', 'moderator'] } })But it returns users who have 'admin' role. Why?
When the field is an array, $nin checks if the entire array value is not in the specified array. It does not check individual elements inside the array. To check if none of the array elements are in a set, use $not with $elemMatch or $nin inside $elemMatch.