Here's a simple and concise guide to CapyDB projection syntax for quick reference:
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.
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.
If no projection is specified:
_id
field will be included if the mode
is exclude
with no fields specified.Example:
{
"mode": "exclude"
}
Explanation: Only the _id
field is returned.
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.
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. |
Your feedback helps us improve our documentation. Let us know what you think!