MongoDB find
时间: 2023-12-05 19:03:56 浏览: 89
The MongoDB find method is used to retrieve documents from a collection in a MongoDB database. It takes a query object as a parameter and returns a cursor object that can be iterated over to retrieve the matching documents. The query object defines the criteria for matching documents and can include conditions on specific fields, operators for comparison or logical operations, and regular expressions for pattern matching. The find method can also accept options such as sorting, limiting, and projection to control the returned results.
Example usage:
```
// Retrieve all documents from the "users" collection
db.users.find()
// Retrieve documents where the "age" field is greater than or equal to 18
db.users.find({ age: { $gte: 18 } })
// Retrieve documents where the "name" field matches a regular expression
db.users.find({ name: /john/i })
// Retrieve documents with projection to include only specific fields
db.users.find({}, { name: 1, email: 1 })
```
阅读全文