Database Reference
In-Depth Information
Here you're basically checking for existence. But there's another way to check for
existence which is practically equivalent: to match an attribute against the null value.
Using this technique, you can alter the preceding queries. The first could be
expressed like this:
db.products.find({'details.color': null})
And the second like this:
db.products.find({'details.color': {$ne: null}})
M ATCHING SUB - DOCUMENTS
Some of the entities in this topic's e-commerce data model have keys that point to a
single embedded object. The product's details attribute is one good example.
Here's part of the relevant document, expressed as JSON :
{ _id: ObjectId("4c4b1476238d3b4dd5003981"),
slug: "wheel-barrow-9092",
sku: "9092",
details: {
model_num: 4039283402,
manufacturer: "Acme",
manufacturer_id: 432,
color: "Green"
}
}
Yoiu can query such objects by separating the relevant keys with a . (dot). For instance,
if you want to find all products manufactured by Acme, you can use this query:
db.products.find({'details.manufacturer_id': 432});
Such queries can be specified arbitrarily deep. Thus, supposing you had the following
slightly modified representation:
{ _id: ObjectId("4c4b1476238d3b4dd5003981"),
slug: "wheel-barrow-9092",
sku: "9092",
details: {
model_num: 4039283402,
manufacturer: { name: "Acme",
id:
432 },
color: "Green"
}
}
The key in the query selector would contain two dots:
db.products.find({'details.manufacturer.id': 432});
But in addition to matching against an individual sub-document attribute, you can
also match an object as a whole. For example, imagine you're using MongoDB to store
market positions. To save space, you forgo the standard object ID and replace it with a
Search WWH ::




Custom Search