Database Reference
In-Depth Information
"Plugge, Eelco",
"Hawkins, Tim"
],
"Publisher" : [
DBRef(“publishercollection”, “Apress”)
]
}
> db.media.save(book)
And that's it! Granted, the example looks a little less simple than the manual method
of referencing data; however, it's a good alternative for cases where collections can
change from one document to the next.
Implementing Index-Related Functions
In the previous chapter, you took a brief look at what indexes can do for your database.
Now it's time to briefly learn how to create and use indexes. Indexing will be discussed in
greater detail in Chapter 10, but for now let's look at the basics. MongoDB includes a fair
number of functions available for maintaining your indexes; we'll begin by creating an
index with the ensureIndex() function.
The ensureIndex() function takes at least one parameter, which is the name of a key
in one of your documents that you will use to build the index. In the previous example,
you added a document to the media collection that used the Title key. This collection
would be well served by an index on this key.
the rule of thumb in MongoDB is to create an index for the same sort of scenarios
where you'd want to create one in MySQL.
Tip
You can create an index for this collection by invoking the following command:
> db.media.ensureIndex( { Title : 1 } )
This command ensures that an index will be created for all the Title values from
all documents in the media collection. The :1 at the end of the line specifies the direction
of the index: 1 stores the items in ascending order, whereas -1 stores them in
descending order.
// Ensure ascending index
db.media.ensureIndex( { Title :1 } )
// Ensure descending index
db.media.ensureIndex( { Title :-1 } )
 
 
Search WWH ::




Custom Search