Use the find() statement in MongoDB to query and filter documents based on query conditions. Syntax: db.collection.find(query, projection). Parameters include optional query conditions (query) and return fields (projection). Usage: Find all documents, conditional search, specify return fields, paging queries, sort results, find array documents, use regular expressions and logical operators for complex queries.
MongoDB Query Statement
MongoDB uses a query statement called find()
Retrieve documents in a collection.
Syntax
<code>db.collection.find(query, projection)</code>
Parameters
{ name: "John" }
. { name: 1, age: 1 }
. Usage
1. Find all documents
<code>db.collection.find()</code>
2. Find documents based on conditions
<code>db.collection.find({ name: "John" })</code>
3. Specify the return field
<code>db.collection.find({}, { name: 1, age: 1 })</code>
4. Paging query
<code>db.collection.find().skip(10).limit(5)</code>
5. Sort Result
<code>db.collection.find().sort({ name: 1 }) // Ascending order db.collection.find().sort({ name: -1 }) // Descending order</code>
6. Find the array in the document
<code>db.collection.find({"arrayField.field": "value"})</code>
7. Use regular expression
<code>db.collection.find({ name: /John/i }) // case-insensitive match</code>
8. Use logical operators
<code>db.collection.find({ $and: [{ name: "John" }, { age: { $gt: 18 }}] }) // AND operator</code>
The above is the detailed content of What is the mongodb query statement called?. For more information, please follow other related articles on the PHP Chinese website!