There seems to be almost no documentation about how to design databases for MongoDB. So I thought I’ll start by posting my questions here.
Assume this collection (fruits_inventory) as an example:
{
"name" : "Orange",
"type" : "citric",
"available" : 3
}
{
"name" : "Apple",
"type" : "pome",
"available" : 0
"note" : "Not shipping this month"
}
{
"name" : "Pear",
"type" : "pome",
"available" : 2
}
(No indexes set)
1) Field selection
db.fruits_inventory.findOne({name:"Orange"},{"note":1});
Will this query seek for a document containing only a field name with value Orange and return with the first hit, even if it has no note field set? Or will it keep searching for a document that does contains a note field?
2) With unique indexes
If I set a unique index on name, would the answer for the previous question change?
Only these two questions for now. Answers will be greatly appreciated.
I wrote the following script:
Then I ran it from the mongo shell and got:
So, the answer is “Yes,” it will return the first hit, even if it has no “note” field set. Adding an index doesn’t change that.