|
| 1 | +# table structure. all content symbolic. |
| 2 | +section: layout |
| 3 | +header: [ meta.header1, meta.header2, meta.header3] |
| 4 | +rows: |
| 5 | + - 1: [ content.sql1, content.mongo1, content.ref1 ] |
| 6 | + - 2: [ content.sql2, content.mongo2, content.ref2 ] |
| 7 | + - 3: [ content.sql3, content.mongo3, content.ref3 ] |
| 8 | + - 4: [ content.sql4, content.mongo4, content.ref4 ] |
| 9 | + - 5: [ content.sql5, content.mongo5, content.ref4 ] |
| 10 | + - 6: [ content.sql6, content.mongo6, content.ref6 ] |
| 11 | +--- |
| 12 | +# table metadata, as meta.<key> |
| 13 | +section: meta |
| 14 | +header1: "SQL Schema Statements" |
| 15 | +header2: "MongoDB Schema Statements" |
| 16 | +header3: "Reference" |
| 17 | +--- |
| 18 | +# table content, as content.<key> |
| 19 | +section: content |
| 20 | +sql1: | |
| 21 | + .. code-block:: sql |
| 22 | +
|
| 23 | + CREATE TABLE users ( |
| 24 | + id MEDIUMINT NOT NULL |
| 25 | + AUTO_INCREMENT, |
| 26 | + user_id Varchar(30), |
| 27 | + age Number, |
| 28 | + status char(1), |
| 29 | + PRIMARY KEY (id) |
| 30 | + ) |
| 31 | +mongo1: | |
| 32 | + Implicitly created on first :method:`insert |
| 33 | + <db.collection.insert()>` operation. The primary key ``_id`` is |
| 34 | + automatically added if ``_id`` field is not specified. |
| 35 | +
|
| 36 | + .. code-block:: javascript |
| 37 | + :emphasize-lines: 1-5 |
| 38 | +
|
| 39 | + db.users.insert( { |
| 40 | + user_id: "abc123", |
| 41 | + age: 55, |
| 42 | + status: "A" |
| 43 | + } ) |
| 44 | +
|
| 45 | + However, you can also explicitly create a table: |
| 46 | +
|
| 47 | + .. code-block:: javascript |
| 48 | + :emphasize-lines: 1 |
| 49 | +
|
| 50 | + db.createCollection("users") |
| 51 | +ref1: | |
| 52 | + See |
| 53 | + :method:`insert() <db.collection.insert()>` and |
| 54 | + :method:`createCollection() <db.createCollection()>` |
| 55 | + for more information. |
| 56 | +sql2: | |
| 57 | + .. code-block:: sql |
| 58 | +
|
| 59 | + ALTER TABLE users |
| 60 | + ADD join_date DATETIME |
| 61 | +mongo2: | |
| 62 | + Update operations can add fields to all documents in a |
| 63 | + collection: |
| 64 | +
|
| 65 | + .. code-block:: javascript |
| 66 | + :emphasize-lines: 1-5 |
| 67 | +
|
| 68 | + db.users.update( |
| 69 | + { }, |
| 70 | + { $set: { join_date: null } }, |
| 71 | + { multi: 1 } |
| 72 | + ) |
| 73 | +
|
| 74 | + Because each :term:`document` determines its own fields, a |
| 75 | + document may contain fields not contained by the other |
| 76 | + documents in the collection. |
| 77 | +ref2: | |
| 78 | + See :method:`update() <db.collection.update()>` and |
| 79 | + :operator:`$set` for more information. |
| 80 | +sql3: | |
| 81 | + .. code-block:: sql |
| 82 | +
|
| 83 | + ALTER TABLE users |
| 84 | + DROP COLUMN join_date |
| 85 | +mongo3: | |
| 86 | + Update operations can remove fields from all documents in a |
| 87 | + collection: |
| 88 | +
|
| 89 | + .. code-block:: javascript |
| 90 | + :emphasize-lines: 1-5 |
| 91 | +
|
| 92 | + db.users.update( |
| 93 | + { }, |
| 94 | + { $unset: { join_date: "" } }, |
| 95 | + { multi: 1 } |
| 96 | + ) |
| 97 | +ref3: | |
| 98 | + See :method:`update() <db.collection.update()>` |
| 99 | + and :operator:`$unset` for more information. |
| 100 | +sql4: | |
| 101 | + .. code-block:: sql |
| 102 | +
|
| 103 | + CREATE INDEX idx_user_id_asc |
| 104 | + ON users(user_id) |
| 105 | +mongo4: | |
| 106 | + .. code-block:: javascript |
| 107 | + :emphasize-lines: 1 |
| 108 | +
|
| 109 | + db.users.ensureIndex( { user_id: 1 } ) |
| 110 | +ref4: | |
| 111 | + See :method:`ensureIndex() <db.collection.ensureIndex()>` |
| 112 | + and :doc:`indexes </core/indexes>` for more information. |
| 113 | +sql5: | |
| 114 | + .. code-block:: sql |
| 115 | +
|
| 116 | + CREATE INDEX idx_user_id_asc_age_desc |
| 117 | + ON users(name, age DESC) |
| 118 | +mongo5: | |
| 119 | + .. code-block:: javascript |
| 120 | + :emphasize-lines: 1 |
| 121 | +
|
| 122 | + db.users.ensureIndex( { user_id: 1, age: -1 } ) |
| 123 | +sql6: | |
| 124 | + .. code-block:: sql |
| 125 | +
|
| 126 | + DROP TABLE users |
| 127 | +mongo6: | |
| 128 | + .. code-block:: javascript |
| 129 | + :emphasize-lines: 1 |
| 130 | +
|
| 131 | + db.users.remove() |
| 132 | +ref6: | |
| 133 | + See :method:`remove() <db.collection.remove()>` for |
| 134 | + more information. |
| 135 | +... |
0 commit comments