|
| 1 | +Documents |
| 2 | +--------- |
| 3 | + |
| 4 | +In python-arango-async, a **document** is an object with the following |
| 5 | +properties: |
| 6 | + |
| 7 | +* Is JSON serializable. |
| 8 | +* May be nested to an arbitrary depth. |
| 9 | +* May contain lists. |
| 10 | +* Contains the ``_key`` field, which identifies the document uniquely within a |
| 11 | + specific collection. |
| 12 | +* Contains the ``_id`` field (also called the *handle*), which identifies the |
| 13 | + document uniquely across all collections within a database. This ID is a |
| 14 | + combination of the collection name and the document key using the format |
| 15 | + ``{collection}/{key}`` (see example below). |
| 16 | +* Contains the ``_rev`` field. ArangoDB supports MVCC (Multiple Version |
| 17 | + Concurrency Control) and is capable of storing each document in multiple |
| 18 | + revisions. Latest revision of a document is indicated by this field. The |
| 19 | + field is populated by ArangoDB and is not required as input unless you want |
| 20 | + to validate a document against its current revision. |
| 21 | + |
| 22 | +For more information on documents and associated terminologies, refer to |
| 23 | +`ArangoDB manual`_. Here is an example of a valid document in "students" |
| 24 | +collection: |
| 25 | + |
| 26 | +.. _ArangoDB manual: https://docs.arangodb.com |
| 27 | + |
| 28 | +.. testcode:: |
| 29 | + |
| 30 | + { |
| 31 | + '_id': 'students/bruce', |
| 32 | + '_key': 'bruce', |
| 33 | + '_rev': '_Wm3dzEi--_', |
| 34 | + 'first_name': 'Bruce', |
| 35 | + 'last_name': 'Wayne', |
| 36 | + 'address': { |
| 37 | + 'street' : '1007 Mountain Dr.', |
| 38 | + 'city': 'Gotham', |
| 39 | + 'state': 'NJ' |
| 40 | + }, |
| 41 | + 'is_rich': True, |
| 42 | + 'friends': ['robin', 'gordon'] |
| 43 | + } |
| 44 | + |
| 45 | +Standard documents are managed via collection API wrapper: |
| 46 | + |
| 47 | +.. code-block:: python |
| 48 | +
|
| 49 | + from arangoasync import ArangoClient |
| 50 | + from arangoasync.auth import Auth |
| 51 | +
|
| 52 | + # Initialize the client for ArangoDB. |
| 53 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 54 | + auth = Auth(username="root", password="passwd") |
| 55 | +
|
| 56 | + # Connect to "test" database as root user. |
| 57 | + db = await client.db("test", auth=auth) |
| 58 | +
|
| 59 | + # Get the API wrapper for "students" collection. |
| 60 | + students = db.collection("students") |
| 61 | +
|
| 62 | + # Create some test documents to play around with. |
| 63 | + lola = {"_key": "lola", "GPA": 3.5, "first": "Lola", "last": "Martin"} |
| 64 | + abby = {"_key": "abby", "GPA": 3.2, "first": "Abby", "last": "Page"} |
| 65 | + john = {"_key": "john", "GPA": 3.6, "first": "John", "last": "Kim"} |
| 66 | + emma = {"_key": "emma", "GPA": 4.0, "first": "Emma", "last": "Park"} |
| 67 | +
|
| 68 | + # Insert a new document. This returns the document metadata. |
| 69 | + metadata = await students.insert(lola) |
| 70 | + assert metadata["_id"] == "students/lola" |
| 71 | + assert metadata["_key"] == "lola" |
| 72 | +
|
| 73 | + # Insert multiple documents. |
| 74 | + await students.insert_many([abby, john, emma]) |
| 75 | +
|
| 76 | + # Check if documents exist in the collection. |
| 77 | + assert await students.has("lola") |
| 78 | +
|
| 79 | + # Retrieve the total document count. |
| 80 | + count = await students.count() |
| 81 | +
|
| 82 | + # Retrieve one or more matching documents. |
| 83 | + async for student in await students.find({"first": "John"}): |
| 84 | + assert student["_key"] == "john" |
| 85 | + assert student["GPA"] == 3.6 |
| 86 | + assert student["last"] == "Kim" |
| 87 | +
|
| 88 | + # Retrieve one or more matching documents, sorted by a field. |
| 89 | + async for student in await students.find({"first": "John"}, sort=[{"sort_by": "GPA", "sort_order": "DESC"}]): |
| 90 | + assert student["_key"] == "john" |
| 91 | + assert student["GPA"] == 3.6 |
| 92 | + assert student["last"] == "Kim" |
| 93 | +
|
| 94 | + # Retrieve a document by key. |
| 95 | + await students.get("john") |
| 96 | +
|
| 97 | + # Retrieve a document by ID. |
| 98 | + await students.get("students/john") |
| 99 | +
|
| 100 | + # Retrieve a document by body with "_id" field. |
| 101 | + await students.get({"_id": "students/john"}) |
| 102 | +
|
| 103 | + # Retrieve a document by body with "_key" field. |
| 104 | + await students.get({"_key": "john"}) |
| 105 | +
|
| 106 | + # Retrieve multiple documents by ID, key or body. |
| 107 | + await students.get_many(["abby", "students/lola", {"_key": "john"}]) |
| 108 | +
|
| 109 | + # Update a single document. |
| 110 | + lola["GPA"] = 2.6 |
| 111 | + await students.update(lola) |
| 112 | +
|
| 113 | + # Update one or more matching documents. |
| 114 | + await students.update_match({"last": "Park"}, {"GPA": 3.0}) |
| 115 | +
|
| 116 | + # Replace a single document. |
| 117 | + emma["GPA"] = 3.1 |
| 118 | + await students.replace(emma) |
| 119 | +
|
| 120 | + # Replace one or more matching documents. |
| 121 | + becky = {"first": "Becky", "last": "Solis", "GPA": "3.3"} |
| 122 | + await students.replace_match({"first": "Emma"}, becky) |
| 123 | +
|
| 124 | + # Delete a document by body with "_id" or "_key" field. |
| 125 | + await students.delete(emma) |
| 126 | +
|
| 127 | + # Delete multiple documents. Missing ones are ignored. |
| 128 | + await students.delete_many([abby, emma]) |
| 129 | +
|
| 130 | + # Delete one or more matching documents. |
| 131 | + await students.delete_match({"first": "Emma"}) |
0 commit comments