MongoDB query from node.js (express-coffeescript-mongodb) -
i'm new database queries nosql databases , have quick question.
essentially want retrieve 'name' of fields have 'publishedimage' set 'true'. store results in array , use while loop print name in unordered list onto .jade template file in view folder.
what query need this? know how done php , mysql, after viewing node tutorials, i'm more confused when started.
i thankful if this!
thanks guys,
joel.
there few things seem asking.
how do query mongodb?
a query in mongodb isn't different sql query. mongodb docs have good examples of how query runs in system. scroll down page, see isn't different have been doing in mysql.
each mongo module has different syntax constructing , executing query. however, ultimately, need execute query shown in mongo docs linked. can see @ mongodb syntax compared module-specific syntax. example, if want find something:
- mongodb find:
db.collection.find( <query>, <projection> )
- node-mongodb find:
collection.find(query, [options])
- mongoose find:
model.find(conditions, [fields], [options], [callback])
you can see calls via modules (must) mimic mongodb query. true, mongoose calls query "conditions" if read mongoose docs, you'll see condition syntax same mongodb syntax.
how store results?
this more complicated question. both node-mongodb , mongoose find
functions listed return query results , each gives options handle returns: in fact, node-mongodb example linked explicitly shows how chain .toarray
call .find
query , voila! working query results array. mongoose gives option of using callback can handle errors , results upon receipt, results being formatted using schema model uses. (mongoose schema , models topic should explore in detail via docs , searches here, if choose use mongoose module.)
how loop through array?
this isn't mongodb question, javascript question. let me point these 2 answers here on stackoverflow:
also, might want @ underscorejs library , it's accompanying underscore node module. each
, map
functions underscorejs handy in situations this.
Comments
Post a Comment