diff --git a/app/views/docs/database.phtml b/app/views/docs/database.phtml index 7f9c136e7..36d10bfb6 100644 --- a/app/views/docs/database.phtml +++ b/app/views/docs/database.phtml @@ -150,7 +150,7 @@ class MainActivity : AppCompatActivity() { val client = Client(applicationContext) .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint - .setProject("5df5acd0d48c2") // Your project ID + .setProject("my_project") // Your project ID val database = Database(client) @@ -248,14 +248,14 @@ class MainActivity : AppCompatActivity() { val client = Client(applicationContext) .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint - .setProject("5df5acd0d48c2") // Your project ID + .setProject("my_project") // Your project ID val database = Database(client) GlobalScope.launch { val response = database.listDocuments( collectionId = "[COLLECTION_ID]", - queries = [Query.equal('title', 'Avatar')], + queries = [Query.equal("title", "Avatar")], ) val json = response.body?.string() } @@ -276,7 +276,7 @@ func main() async throws{ let database = Database(client) let list = try await database.listDocuments( collectionId: "[COLLECTION_ID]", - queries: [Query.equal('title', 'Avatar')] + queries: [Query.equal("title", "Avatar")] ) print(list.toMap()) } @@ -376,7 +376,7 @@ class MainActivity : AppCompatActivity() { val client = Client(applicationContext) .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint - .setProject("5df5acd0d48c2") // Your project ID + .setProject("my_project") // Your project ID val database = Database(client) @@ -384,8 +384,8 @@ class MainActivity : AppCompatActivity() { val response = database.listDocuments( collectionId = "[COLLECTION_ID]", queries = [ - Query.equal('title', ['Avatar', 'Lord of the Rings']), - Query.greater('year', 1999), + Query.equal("title", ["Avatar", "Lord of the Rings"]), + Query.greater("year", 1999), ], ) val json = response.body?.string() @@ -408,8 +408,8 @@ func main() async throws { let list = try await database.listDocuments( collectionId: "[COLLECTION_ID]", queries: [ - Query.equal('title', ['Avatar', 'Lord of the Rings']), - Query.greater('year', 1999) + Query.equal("title", ["Avatar", "Lord of the Rings"]), + Query.greater("year", 1999) ] ) print(list.toMap()) @@ -453,5 +453,201 @@ func main() async throws { +
When querying using the listDocuments endpoint, you can specify the order of the documents returned by providing the orderAttributes and orderTypes parameters. The results will be sorted by $id (create date) in ascending order when no order parameters are provided.
+ +The parameter orderAttributes takes a string array of attributes IDs. The orderTypes parameter takes a string array equal in length to orderAttributes that indicate if each attribute should be sorted in ascending or descending order. Ascending and descending is denoted as "ASC" and "DESC" respectively.
+ + +let promise = appwrite.database.listDocuments(
+ 'movies', // collectionId
+ [], // queries
+ 25, // limit
+ 0, // offset
+ '', // cursor
+ 'after', // cursorDirection
+ ['title'], // orderAttributes
+ ['ASC'] // orderTypes
+);
+ import 'package:appwrite/appwrite.dart';
+
+void main() async {
+ final client = Client();
+ final database = Database(client);
+ try {
+ final docs = await database.listDocuments(
+ collectionId: 'movies',
+ orderAttributes: ['title'],
+ orderTypes: ['ASC']
+ );
+ print(docs.toMap());
+ } on AppwriteException catch(e) {
+ print(e);
+ }
+}
+ import androidx.appcompat.app.AppCompatActivity
+import android.os.Bundle
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
+import io.appwrite.Client
+import io.appwrite.services.Database
+import io.appwrite.Query
+
+class MainActivity : AppCompatActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ setContentView(R.layout.activity_main)
+
+ val client = Client(applicationContext)
+ .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
+ .setProject("my_project") // Your project ID
+
+ val database = Database(client)
+
+ GlobalScope.launch {
+ val response = database.listDocuments(
+ collectionId = "movies",
+ orderAttributes = listOf("title"),
+ orderTypes = listOf("ASC")
+ )
+ val json = response.body?.string()
+ }
+ }
+}
+ import Appwrite
+
+func main() async throws {
+ let client = Client()
+ .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
+ .setProject("my_project") // Your project ID
+
+ let database = Database(client)
+ let list = try await database.listDocuments(
+ collectionId: "movies",
+ orderAttributes: ["title"],
+ orderTypes: ["ASC"]
+ )
+ print(list.toMap())
+}
+ To sort based on multiple attributes, pass multiple attribute IDs into orderAttributes. The order of the attribute IDs indicate their sort precedence. There must be an index with all attributes used to allow sorting with multiple attributes.
+ +let promise = appwrite.database.listDocuments(
+ 'movies', // collectionId
+ [], // query
+ 25, // limit
+ 0, // offset
+ '', // cursor
+ 'after', // cursorDirection
+ ['title', 'year'], // orderAttributes
+ ['ASC', 'DESC'] // orderTypes
+);
+ import 'package:appwrite/appwrite.dart';
+
+void main() async {
+ final client = Client();
+ final database = Database(client);
+ try {
+ final docs = await database.listDocuments(
+ collectionId: 'movies',
+ orderAttributes: ['title', 'year'],
+ orderTypes: ['ASC', 'DESC']
+ );
+ print(docs.toMap());
+ } on AppwriteException catch(e) {
+ print(e);
+ }
+}
+ import androidx.appcompat.app.AppCompatActivity
+import android.os.Bundle
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
+import io.appwrite.Client
+import io.appwrite.services.Database
+import io.appwrite.Query
+
+class MainActivity : AppCompatActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ setContentView(R.layout.activity_main)
+
+ val client = Client(applicationContext)
+ .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
+ .setProject("my_project") // Your project ID
+
+ val database = Database(client)
+
+ GlobalScope.launch {
+ val response = database.listDocuments(
+ collectionId = "movies",
+ orderAttributes = listOf("title", "year"),
+ orderTypes = listOf("ASC", "DESC")
+ )
+ val json = response.body?.string()
+ }
+ }
+}
+ import Appwrite
+
+func main() async throws {
+ let client = Client()
+ .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
+ .setProject("my_project") // Your project ID
+
+ let database = Database(client)
+ let list = try await database.listDocuments(
+ collectionId: "movies",
+ orderAttributes: ["title", "year"],
+ orderTypes: ["ASC", "DESC"]
+ )
+ print(list.toMap())
+}
+ In the example above, the movies returned will be first sorted by title in ascending order, then sorted by year in descending order.
+Appwrite has full support for pagination to better optimize and scale up your applications built on Appwrite. Detailed documentation on pagination and how to implement it can be found on the pagination guide.