Create a Collection
POST
/api/collections/createLike most databases, WorqDB, logically groups data as collections. You can use the createCollection
method to create a collection. The method takes in the name of the collection as the first argument, and several optional configuration options as the second and third arguments.
The parameters that we can pass to the createCollection
method are:
name
- The name of the collection to create.structure
- An object containing the Schema of the collection. This is optional. If not provided, the collection will be created and the structure will be defined along the way.defaultSort
- An object containing the default sort order of the collection. This is optional. If not provided, the collection will be created and the default sort order will be defined along the way.
WorqDB is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in documents, which are organized into collections.
Each document contains a set of key-value pairs. WorqDB is optimized for storing large collections of large documents.
All documents must be stored in collections. Documents can contain nested objects, which can include primitive fields like strings.
Collections and documents are created explicitly in WorqDB. You need to create a collection before you can add documents to it. Since WorqDB is schemaless, you can create a collection without specifying the structure of the documents it will contain. Also, WorqDB is a database that is optimized for storing large collections of large documents. Therefore, you need to explicitly declare if you are adding a new document to a collection or updating an existing document.
Creating a Collection
With a Defined structure
As we have proceeded ahead with a schemaless approach, we do not enforce any data types on the fields. However, you can still declare the data types for the fields during the collection creation. This will help you to keep track of the data types of the fields in your collection.
Here is a list of the Data Types that are automatically identified and data types that you have to declare explicitly during document creation.
Data Type | Automatically Identified | Explicitly Declared |
---|---|---|
string |
✅ | ✅ |
number |
✅ | ✅ |
float |
✅ | ✅ |
boolean |
✅ | ✅ |
date |
❌ (Identified as a String) | ✅ |
timestamp |
❌ (Identified as an Integer) | ✅ |
json |
✅ | ✅ |
array |
✅ | ✅ |
geopoint |
❌ (Identified as an Array) | ✅ |
uuid |
✅ | ✅ |
ip |
✅ | ✅ |
You can read more about the Data Types in the Data Types section.
Here is an example of creating a collection with a defined structure:
curl --request POST \
--url https://api.worqhat.com/api/collections/create \
--header 'Authorization: Bearer <API KEY>' \
--header 'Content-Type: application/json' \
--data '{
"collection": "Sample Collection",
"collectionSchema": {
"Name": "string",
"Phone": "number",
"Metadata": "json",
"User ID": "uuid"
},
"collectionSortBy": "Name"
}'
Without a Defined structure
You can also create a collection without defining the structure of the collection. This will allow you to add documents to the collection without having to define the structure of the collection beforehand. You can just keep on adding data in the form of Data Object Documents to the collection and we will keep defining the structure along the way.
You can read more about Data Object Documents in the Data Typessection and how we identify the data types of the fields in the Data Types section.
Here is an example of creating a collection without a defined structure:
curl --request POST \
--url https://api.worqhat.com/api/collections/create \
--header 'Authorization: Bearer <API KEY>' \
--header 'Content-Type: application/json' \
--data '{
"collection": "Sample Collection"
}'
Request
The Name of the Collection or the Group where you want to store all your data.
The Schema of the Collection. This is used to validate the data that is being stored in the Collection. This is optional. You can pass in the variables on how you want to structure the data as. We currently support Data Types such as string
, number
, float
, boolean
, date
, timestamp
, json
, map
, array
, geopoint
, uuid
, ip
.
The Field by which you want to sort the data in the Collection. By default we will sort it by the Document Creation Time of the data that you add.
{
"collection": "Users",
"collectionSchema": {
"name": "string",
"age": "number",
"weight": "float",
"is_active": "boolean",
"date_of_birth": "date",
"timestamp": "timestamp",
"address": "json",
"hobbies": "array",
"location": "geopoint",
"id": "uuid",
"ip_address": "ip"
},
"collectionSortBy": "name"
}
Request samples
Responses
{
"message": "Collection \"Users Data\" created successfully.",
"processingTime": 448.159625,
"processingId": "1d447ab7-49b5-4f6c-8f81-a4cb3ce7eba2"
}