Database Reference
In-Depth Information
Operations
In an online gaming system, with the state embedded in a single document for
character
,
item
, and
location
, the primary operations we'll be performing are as follows:
▪ Querying for the character state by
_id
▪ Extracting relevant data for display
▪ Updating various attributes about the character
This section describes procedures for performing these queries, extractions, and updates. In
particular, we will avoid loading the
location
or
item
documents except when absolutely
necessary.
Load Character Data from MongoDB
The most basic operation in this system is loading the character state:
>>>
>>>
character
=
db
.
characters
.
find_one
({
'_id'
:
character_id
})
In this case, the default index that MongoDB supplies on the
_id
field is sufficient for good
performance of this query.
Extract Armor and Weapon Data for Display
In order to save space, the
character
schema just described stores item details only in the
inventory
attribute, storing
ObjectId
s in other locations. To display these item details, as on
a character summary window, we need to merge the information from the
armor
and
weapons
attributes with information from the
inventory
attribute.
Suppose, for instance, that our code is displaying the armor data using the following Jinja2
template:
<div>
<div>
<h2>
<h2>
Armor
</h2>
</h2>
<dl>
<dl>
{% if value.head %}
<dt>
<dt>
Helmet
</dt>
</dt>