Browsing the archives for the Querying category

Quick Tip: How to $size up a MongoDB Array

in Querying, Quick Tip

TweetSince MongoDB will allow you to store more than just string and int values but also things like arrays … from time to time you might need to know how many items are in an array in your document. For example, say you have a hosting business and to make a good profit you need [...]

0 Comments

Quick Tip: MongoDB Distinct Count

in Querying, Quick Tip

TweetOne query SQL users are pretty used to writing is DISTINCT with COUNT() to get the number if distinct (unique) rows that match a statement like … SELECT COUNT(DISTINCT(PageURL)) FROM LogTable; This rather simple query will get us back the number of unique PageURL’s in our LogTable … not an amazingly useful query but you get [...]

4 Comments

Quick Tip: Return Only 1 Field

in Querying, Quick Tip

TweetSay you have a log of hits to your podcast, something like … { “_id” : ObjectId(“4c6c753800ae261d80000009″), “podcastID” : 1, “remoteUserIP” : “10.40.52.55″, “remoteUserSession” : “gpn1vz1crveoqtgtlbh1tgd1″, “remoteUserAgent” : “iTunes/9.2.1 (Macintosh; Intel Mac OS X 10.6.4) AppleWebKit/533.17.8″, “created” : { “date” : “8/18/2010″, “time” : “5:05:12 PM” } } To get a quick snapshot of hits [...]

2 Comments

Reaching Inside With Dots and $elemMatch

in Querying

TweetOne really awesome thing about MongoDB is you can store more then just basic values and types as attributes in your Document like … “Title” : “Blog Post 1″ You can also store arrays and even mores Documents (aka a sub Document/ Subobjects, see below) … This however can be confusing if you are used to [...]

1 Comment

Good to the Last $slice

in Querying

TweetWhat if we want to query just a subset of elements in an array or sub-document? Well let me tell tell you how MongoDB will slice, will dice, will … Okay let’s just focus on the $slice operator (you will need at least MongoDB 1.5.1) … Our Pizza Dinner Suppose you have a pizza document [...]

0 Comments