2. Collection creation and validation

In the MongoDB philosophy each record is a complete document, containing all of its information, specified in the form of a field and a value for each information.

In this way, all the information in a WordPress page goes to the same document, even though it may be contained in several tables in MySQL. In turn, the MongoDB documents are contained in collections, which are contained in a database.

The collection was named pages, because it contains the WordPress pages. In the collection creation command, the validation rules were also specified, as shown below:

// Collection pages creation specifying the JSON schema validation rules
db.createCollection("pages", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: [ "id", "title", "date", "content", "excerpt", "slug" ],
         properties: {
            id: {
               bsonType: "int",
               description: "page identifier"
            },
            title: {
               bsonType: "string",
               description: "page title"
            },
            date: {
               bsonType: "string",
               description: "page creation date (string)"
            },
            pagedate: {
               bsonType: "date",
               description: "page creation date (date)"
            },
            content: {
               bsonType: "string",
               description: "page content"
            },
            excerpt: {
               bsonType: "string",
               description: "page excerpt"
            },
            slug: {
               bsonType: "string",
               description: "page slug"
            },
            thumb: {
               bsonType: "string",
               description: "page thumb"
            },
            ancestors: {
               bsonType: "array",
               description: "ancestors pages (hierarchically above)"
            },
            coordinates: {
               bsonType: "array",
               description: "page coordinates"
            },
            geometry: {
               bsonType: "object",
               description: "page geometry"
            },
         }
      }
   }
}, { collation: { locale: "pt", strength: 1 } } );

Execution of this commands in the mongo shell:

> // Collection pages creation specifying the JSON schema validation rules
> db.createCollection("pages", {
...    validator: {
...       $jsonSchema: {
...          bsonType: "object",
...          required: [ "id", "title", "date", "content", "excerpt", "slug" ],
...          properties: {
...             id: {
...                bsonType: "int",
...                description: "page identifier"
...             },
...             title: {
...                bsonType: "string",
...                description: "page title"
...             },
...             date: {
...                bsonType: "string",
...                description: "page creation date (string)"
...             },
...             pagedate: {
...                bsonType: "date",
...                description: "page creation date (date)"
...             },
...             content: {
...                bsonType: "string",
...                description: "page content"
...             },
...             excerpt: {
...                bsonType: "string",
...                description: "page excerpt"
...             },
...             slug: {
...                bsonType: "string",
...                description: "page slug"
...             },
...             thumb: {
...                bsonType: "string",
...                description: "page thumb"
...             },
...             ancestors: {
...                bsonType: "array",
...                description: "ancestors pages (hierarchically above)"
...             },
...             coordinates: {
...                bsonType: "array",
...                description: "page coordinates"
...             },
...             geometry: {
...                bsonType: "object",
...                description: "page geometry"
...             },
...          }
...       }
...    }
... }, { collation: { locale: "pt", strength: 1 } } );
{ "ok" : 1 }
>

Note: The field strength: 1 specifies primary level of comparison. Collation performs comparisons of the base characters only, ignoring other differences such as diacritics and case.

Integration of MongoDB with WordPress