Database Reference
In-Depth Information
Since our queries tend to use both
metadata.parent_id
and
metadata.slug
, a unique index
on this combination is sufficient to get good performance:
>>>
>>>
db
.
cms
.
assets
.
files
.
ensure_index
([
...
...
(
'metadata.parent_id'
,
1
), (
'metadata.slug'
,
1
)],
unique
=
True
)
Locate and render a node
To locate a “normal” node based on the value of
metadata.parent_id
and
metadata.slug
,
we can use the
find_one
operation rather than
find
:
node
=
db
.
nodes
.
find_one
({
'metadata.parent_id'
:
parent_id
,
'metadata.slug'
:
slug
})
To locate an image based on the value of
metadata.parent_id
and
metadata.slug
, we use
the GridFS method
get_version
:
code
,
sourceCode
,
python
fs
=
GridFS
(
db
,
'cms.assets'
)
with
with
fs
.
get_version
({
'metadata.parent_id'
:
parent_id
,
'metadata.slug'
:
slug
})
aas
img_fpo
:
# do something with the image file
Search for nodes by tag
To retrieve a list of “normal” nodes based on their tags, the query is straightforward:
nodes
=
db
.
nodes
.
find
({
'metadata.tags'
:
tag
})
To retrieve a list of images based on their tags, we'll perform a search on
cms.assets.files
directly:
image_file_objects
=
db
.
cms
.
assets
.
files
.
find
({
'metadata.tags'
:
tag
})
fs
=
GridFS
(
db
,
'cms.assets'
)
for
for
image_file_object
iin
db
.
cms
.
assets
.
files
.
find
(
{
'metadata.tags'
:
tag
}):
image_file
=
fs
.
get
(
image_file_object
[
'_id'
])
# do something with the image file