Database Reference
In-Depth Information
For example, assume you want to search for a DVD within your collection that is
older than 1995. All of the following code examples would return this information:
db.media.find ( { "Type" : "DVD", "Released" : { $lt : 1995 } } )
db.media.find ( { "Type" : "DVD", $where: "this.Released < 1995" } )
db.media.find ("this.Released < 1995")
f = function() { return this.Released < 1995 }
db.media.find(f)
And that's how flexible MongoDB is! Using these operators should enable you to find
just about anything throughout your collections.
Leveraging Regular Expressions
Regular expressions are another powerful tool you can use to query information. Regular
expressions regex , for short—are special text strings that you can use to describe your
search pattern. These work much like wildcards, but they are far more powerful and
flexible.
MongoDB allows you to use these regular expressions when searching for data in
your collections; however, it will attempt to use an index whenever possible for simple
prefix queries.
The following example uses regex in a query to find all items in the media collection
that start with the word “Matrix”:
> db.media.find ( { Title : /Matrix*/i } )
Using regular expressions from MongoDB can make your life much simpler, so we'd
recommend exploring this feature in greater detail as time permits or your circumstances
can benefit from it.
Updating Data
So far you've learned how to insert and query for data in your database. Next, you'll learn
how to update that data. MongoDB supports quite a few update operators that you'll
learn how to use in the following sections.
Updating with update()
MongoDB comes with the update() function for performing updates to your data. The
update() function takes three primary arguments: criteria , objNew and options .
 
Search WWH ::




Custom Search