1. `$in` operator
1. Function
The `$in` operator is used to match documents whose field values are equal to any value in the specified array, and can batch find multiple documents with specific values.
2. Syntax example
({ field: { $in: [value1, value2, ...] } });
3. Code example
Suppose there is a collection called `users` that contains user information. To find a user whose `age` is 20, 25, or 30, you can use the following code:
({ age: { $in: [20, 25, 30] } });
2. `$nin` operator
1. Function
`$nin` is an inverse operator of `$in` to match documents whose field values are not in the specified array, and can batch exclude multiple documents with specific values.
2. Syntax example
({ field: { $nin: [value1, value2, ...] } });
3. Code example
Or in the `users` collection, to find users whose `age` is not 20, 25, or 30, you can use the following code:
({ age: { $nin: [20, 25, 30] } });
3. `$or` operator
1. Function
The `$or` operator can combine multiple query conditions. As long as a document that meets one of the conditions is returned, it can be used to batch searches for documents that meet multiple different conditions.
2. Syntax example
({ $or: [ { condition1 }, { condition2 }, ... ] });
3. Code example
In the `users` collection, to find users whose `age` is 20 or `name` is "John", you can use the following code:
({ $or: [{ age: 20 }, { name: "John" }], });
4. `$and` operator
1. Function
The `$and` operator combines multiple query conditions. The document must meet all conditions at the same time before it is returned. It is often used to batch searches for documents that meet multiple conditions at the same time.
2. Syntax example
({ $and: [ { condition1 }, { condition2 }, ... ] });
3. Code example
In the `users` collection, to find users whose `age` is greater than 20 and `name` is "John", you can use the following code:
({ $and: [{ age: { $gt: 20 } }, { name: "John" }], });
5. Range query operators (`$gt`, `$lt`, `$gte`, `$lte`)
1. Function
These operators can batch search documents with field values within a certain range. `$gt` means greater than, `$lt` means less than, `$gte` means greater than or equal, and `$lte` means less than or equal.
2. Syntax example
// Greater than({ field: { $gt: value } }); // Small({ field: { $lt: value } }); // Greater than or equal to({ field: { $gte: value } }); // Less than or equal to({ field: { $lte: value } });
3. Code example
In the `users` collection, to find users with `age` greater than 20 and less than 30, use the following code:
({ age: { $gt: 20, $lt: 30 } });
This is the end of this article about MongoDB's batch search symbols. For more related MongoDB's batch search symbol content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!