Document
Find

Find

OneNode DB allows you to query and retrieve documents from a collection using operations similar to MongoDB's find() functionality. You can use filters to match specific documents, and optionally, specify projection fields, sort order, and pagination options. This guide explains how to use the /find endpoint via the REST API.

Parameters for Find Operations

  • filter: A query object to match the documents to retrieve. This works the same way as MongoDB filters, allowing you to specify conditions to find the documents. For more details, refer to the official MongoDB documentation on query filters (opens in a new tab).
  • projection (optional): A field specification object to control which fields are returned in the result set. Use 1 to include and 0 to exclude fields.
  • sort (optional): An object specifying the sort order for the result set (e.g., {"age": 1} to sort by age in ascending order, or {"age": -1} for descending).
  • limit (optional): A number to limit the number of documents returned.
  • skip (optional): A number to skip the first n documents in the result set, useful for pagination.

find Operation

The find operation is used to query multiple documents in a collection based on a filter. You can optionally include projection, sort, limit, and skip parameters for more advanced queries.

Endpoint

To query documents, use the following endpoint:

{collection_url}/find

Where {collection_url} is the full collection URL that includes your db_id and collection_name.

Example Python Code for find

Here’s how you can query documents using Python, by applying a filter, projection, and sort operation:

import os
import requests
 
# API Key and Collection URL
ONENODE_API_KEY = os.getenv('ONENODE_API_KEY')
collection_url = "https://api.onenode.ai/v1/db/123abc/collection/my_collection"
 
# Find URL
url = f"{collection_url}/find"
 
# Filter to match the document(s)
filter = {
    "age": {"$gt": 25}
}
 
# Optional projection, sort, limit, and skip parameters
projection = {
    "name": 1,
    "age": 1,
    "_id": 0  # Exclude _id
}
 
sort = {
    "age": -1  # Sort by age in descending order
}
 
limit = 5
skip = 0
 
# Request body
data = {
    "filter": filter,
    "projection": projection,
    "sort": sort,
    "limit": limit,
    "skip": skip
}
 
# Headers with API Key
headers = {
    "Authorization": f"Bearer {ONENODE_API_KEY}",
    "Content-Type": "application/json"
}
 
# Sending the request
response = requests.post(url, json=data, headers=headers)
 
# Print the response
print(response.json())

find Response

A successful find operation will return a JSON response containing the matching documents in the collection. Here’s an example response:

{
  "status": "success",
  "documents": [
    {
      "name": "Alice Smith",
      "age": 29
    },
    {
      "name": "Bob Johnson",
      "age": 40
    }
  ]
}

Important Notes

  • filter: The filter works similarly to MongoDB’s query language, allowing you to specify conditions for retrieving documents. For more details, refer to the official MongoDB documentation on query filters (opens in a new tab).
  • projection: Use the projection parameter to control which fields are returned in the result set. Use 1 to include and 0 to exclude fields.
  • sort: You can use the sort parameter to order the result set by one or more fields in ascending (1) or descending (-1) order.
  • limit and skip: These are optional parameters for controlling the number of documents returned and for pagination.