mongodb find
时间: 2023-10-04 16:07:42 浏览: 113
The MongoDB `find()` method is used to retrieve documents from a collection based on certain criteria. It takes in a query object as a parameter, which specifies the conditions that the documents must meet to be returned.
The basic syntax of the `find()` method is as follows:
```
db.collection.find(query, projection)
```
Here, `collection` is the name of the collection to search in, `query` is the query object that specifies the conditions for matching documents, and `projection` is an optional parameter that specifies which fields to include or exclude in the returned documents.
For example, to find all documents in a collection named `users` where the `age` field is greater than or equal to 18, we can use the following query:
```
db.users.find({ age: { $gte: 18 } })
```
This will return all documents in the `users` collection where the `age` field is greater than or equal to 18.
We can also use other operators like `$lt` (less than), `$gt` (greater than), `$ne` (not equal), etc. to specify more complex queries. Additionally, we can use the `projection` parameter to specify which fields to include or exclude in the returned documents. For example, to exclude the `_id` field from the returned documents, we can use the following query:
```
db.users.find({ age: { $gte: 18 } }, { _id: 0 })
```
阅读全文