Skip to content

Getting Data from MongoDB

Steve Gongage edited this page Aug 12, 2014 · 4 revisions

Start off with the collection

The following examples use the 'people' collection which is created as follows

collection = "people"; people = mongo.getDBCollection('people');

Using Query()

marc = people.query().$eq("NAME", "Marc").find();

find riders of Specialized bikes

specialized = people.query().$eq("BIKE", "Specialized").find();

find the 3rd and 4th Specialized bike riders, sorted by "ts" descending

specialized = people.query().$eq("BIKE", "Specialized").find( skip=2, limit=2, sort={"TS"=-1} );

find riders with counter between 1 and 3, sorted by "ts" descending

specialized = people.query() .$eq("BIKE", "Specialized") .between("COUNTER", 1, 3) .find( sort={"TS"=-1} );

find riders with counter between 1 and 3 Exclusive, sorted by "ts" descending

specialized = people.query() .$eq("BIKE", "Specialized") .betweenExclusive("COUNTER", 1, 3) .find( sort={"TS"=-1});

find people with kids aged between 2 and 30

kidSearch = people.query().between("KIDS.AGE", 2, 30).find(keys="NAME,COUNTER,KIDS", sort={"COUNTER"=-1});

find a document by ObjectID

note that it returns the document, NOT a SearchResult object; here, we'll "spoof" what your app would do if the id were in the URL scope

url.personId = specialized.asArray()[1]["_id"].toString();

Using FindByID

byID = people.findById( url.personId );

Clone this wiki locally