results matching ""
No results matching ""
1.4.Database
X-Server provides high performance data persistence. In particular, Support for embedded data models reduces I/O activity on database system. Indexes support faster queries and can include keys from embedded documents and arrays.
¶ 1.4.1. Collection and Documents
In X-Server, databases hold collections of documents. Collections are analogous to tables in relational databases, and documents are analogous to record in relational databases. X-Sever stores documents in collections. A record in X-Server is a document, which is a data structure composed of field and value pairs. X-Server documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents. Document structure is composed of field-and-value pairs and have the following structure:
{
field1: value1,
field2: value2,
field3: value3,
...
fieldN: valueN
}
Field names are strings. The value of a field can be any of the JSON data types, including other documents, arrays, and arrays of documents.
For example, the following document contains values of varying types:
var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"),
name: { first: "Alan", last: "Turing" },
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}
The above fields have the following data types:
- _id holds an ObjectId.
- name holds an embedded document that contains the fields first and last.
- birth and death hold values of the Date type.
- contribs holds an array of strings.
- views holds a value of the NumberLong type.
Documents have the following restrictions on field names:
The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array. Field names cannot contain the null character.
Top-level field names cannot start with the dollar sign ($) character.
Otherwise, the server permits storage of field names that contain dots (i.e. .) and dollar signs (i.e. $). The use of $ and . in field names is not recommended and is not supported.
The advantages of using documents are: Documents (i.e. objects) correspond to native data types in many programming languages. Embedded documents and arrays reduce need for expensive joins. Dynamic schema supports fluent polymorphism.
¶ 1.4.2. Select a Collection
To select a collection to use, in the HTML file, issue the use
tim.X("collection_name")
¶ 1.4.3. Create a Collection
If a collection does not exist, X-Server creates the collection when you first store data for that collection. As such, you can switch to a non-existent collection and perform the following operation in the HTML file:
tim.X("NewCollection").New( { name: "tim" } )
The New() operation creates both the collection NewCollection and the document { name: "tim" } if they do not already exist.
¶ 1.4.4. Document Validation
By default, a collection does not require its documents to have the same schema; i.e. the documents in a single collection do not need to have the same set of fields and the data type for a field can differ across documents within a collection. However, you can enforce document validation rules for a collection during update and insert operations.
tim.X("NewCollection").Set({fieldName:'name', unique: true });
¶ 1.4.5. Modifying Document Structure
To change the structure of the documents in a collection, such as add new fields, remove existing fields, or change the field values to a new type, update the documents to the new structure.
tim.X("NewCollection").Mix({newField:"newValue" });
¶ 1.4.6. Automatic Unique Identifiers
Each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, it will be automatically assigned an immutable _id field by default.
This also applies to documents inserted through update operations with upsert: true.
The _id field has the following behavior and constraints:
By default, X-Server creates a unique index on the _id field during the creation of a collection.
The _id field is always the first field in the documents. If the server receives a document that does not have the _id field first, then the server will move the field to the beginning.
The _id field may contain values of any JSON data type, other than an array.
tim.X("NewCollection").ID("7gFV1pzAfp6uHr5E");
The following are common options for storing values for _id:
Use an ObjectId.
Use a natural unique identifier, if available. This saves space and avoids an additional index.
Generate an auto-incrementing number.
Generate a UUID in your application code.
To Be Continued
This page is just an unfinished draft. The author is writing now. Welcome to donate, cheer for the author.
小礼物刷一波,打赏作者

Paypal Donate

Venmo Donate

WeChat微信打赏

Alipay支付宝打赏