简体   繁体   中英

MongoDB query from node.js (express-coffeescript-mongodb)

I'm new to database queries with noSQL databases and have a quick question.

Essentially I want to retrieve the 'name' of all fields that have 'publishedImage' set to 'true'. I would like to store the results in an array and then use something like a while loop to print the name in a unordered list onto a .jade template file in the view folder.

What query would I need to do this? I know how this would be done with say php and mysql, but after viewing node tutorials, I'm more confused then when I started.

I would be very thankful if I could get some help with this!

Thanks guys,

Joel.

There are a few things you seem to be asking.

How do I do a query with mongodb?

A query in mongoDB isn't really that much different than a SQL query. The mongoDB docs have some good examples of how the query runs in the system. As you scroll down that page, you will see that it really isn't that much different than what you would have been doing in mysql.

Each mongo module has different syntax for constructing and executing the query. However, ultimately, they all need to execute a query as is shown in the mongo docs I linked. You can see this as you look at the mongodb syntax compared to module-specific syntax. For example, if we want to find something:

You can see the calls via the modules (must) mimic the mongodb query. True, mongoose calls the query "conditions" but if you read the mongoose docs, you'll see the condition syntax is essentially the same as the mongoDB syntax.

How do I store the results?

This is a more complicated question. Both node-mongodb and mongoose find functions I listed return query results and each gives some options to handle the returns: in fact, the node-mongodb example I linked to explicitly shows you how to chain a .toArray call to the .find query and voila! you are working with your query results array. Mongoose gives the option of using a callback so you can handle errors and results upon receipt, with the results being formatted using the Schema the Model uses. (Mongoose schema and models are another topic which you should explore in detail via the docs and searches here, if you choose to use the mongoose module.)

How do I loop through the array?

This isn't really a mongoDB question, but is a javascript question. Let me point you to these two answers here on StackOverflow:

  1. Loop through an array in javascript
  2. Loop through javascript object

Also, you might want to look at the underscorejs library and it's accompanying underscore node module. The each and map functions from underscorejs are handy in situations like this.

I also came from mysql databases. I'm actually using mongoose to manage mongoDB from node.js and it was easy to learn just reading this doc: http://mongoosejs.com/docs/index.html

The find function is the one you want. With something like this:

MyStuff.find({ publishedImage: true }, function(error,result){
  if(!error){
    // Do stuff with result
  }
});

"result" will be an array with all your wanted documents.

Mongoose has many options, but with the doc from the link, you can learn the basics to start. Hope it helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM