OneNode Projection Syntax (Simplified Guide)
Here's a simple and concise guide to OneNode projection syntax for quick reference:
1. Include Fields
Specify fields to include in the query response by setting the mode to include and listing fields.
{
"mode": "include",
"fields": ["field1", "field2"]
}Example:
{
"mode": "include",
"fields": ["name", "bio"]
}Explanation: Only the name and bio fields will be returned.
2. Exclude Fields
Specify fields to exclude in the query response by setting the mode to exclude and listing fields.
{
"mode": "exclude",
"fields": ["field1", "field2"]
}Example:
{
"mode": "exclude",
"fields": ["password", "creditCard"]
}Explanation: All fields except password and creditCard will be returned.
3. Default Behavior
If no projection is specified:
- The entire document will be included in the response by default.
- Only the
_idfield will be included if themodeisexcludewith no fields specified.
Example:
{
"mode": "exclude"
}Explanation: Only the _id field is returned.
4. Nested Fields
Use dot notation to specify nested fields.
{
"mode": "include",
"fields": ["address.city", "address.zip"]
}Explanation: Only the city and zip fields within address will be included.
Projection Parameter Scenarios
| Example Projection Value | Result |
|---|---|
{ "mode": "include" } | The entire document is returned. |
{ "mode": "include", "fields": ["title", "author"] } | Only the title and author fields are returned. |
{ "mode": "exclude" } | Only the _id field is returned. |
{ "mode": "exclude", "fields": ["title", "author"] } | All fields except title and author are returned. |
How can we improve this documentation?
Share Your Thoughts
Your feedback helps us improve our documentation. Let us know what you think!