Node.JS,Express&MongoDB ::多个集合 [英] Node.JS, Express & MongoDB :: Multiple Collections
问题描述
我正在使用Node.JS,Express,MongoDB和EJS.我有一个问题,关于一个数据库下的多个MongoDB集合,并要求它们在前端使用.我有3个收藏夹,经过大量研究后仍未找到如何正确调用多个收藏夹的方法.
I'm using Node.JS, Express, MongoDB and EJS. I have a question regarding multiple MongoDB collections under one database, and calling them to use on the front-end. I have 3 collections, and after much research haven't found how to call more than one collection without errors.
我知道将所有内容存储在一个集合中并不是什么问题,但是我觉得最好的方法是将它们按类别分开.这是我的数据库/收集呼叫:
I understand that it wouldn't be a problem to store everything under one collection, but I feel like the best approach would be to separate them out by category. Here's my db/collection call:
app.use(express.bodyParser());
var MongoClient = require('mongodb').MongoClient;
app.get('/', function(req, res){
MongoClient.connect("mongodb://localhost:27017/michael", function(err, db) {
if(!err) {
console.log("We are connected");
}
db.collection("portfolio", function(err, collection) {
collection.find().sort({order_num: 1}).toArray(function(err, result) {
var portfolio = [];
if (err) {
throw err;
} else {
for (i=0; i<result.length; i++) {
portfolio[i] = result[i];
}
res.render('index.html', {portfolio: portfolio});
}
});
});
});
});
我如何才能多次调用其他收藏集,例如关于"收藏集等?我尝试添加另一个...
How could I make multiple calls to additional collections, such as an "about" collection, etc? I tried adding another...
db.collection("about", function(err, collection) {
...在...内...
...within...
MongoClient.connect("mongodb://localhost:27017/michael", function(err, db) {
...但是在终端控制台日志中却给我错误.
...but it gives me errors in the Terminal console log.
谢谢.
这就是我想要做的,这给我一个错误:
Here's what I'm trying to do, which gives me an error:
app.get('/', function(req, res){
MongoClient.connect("mongodb://localhost:27017/michael", function(err, db) {
if(!err) {
console.log("We are connected");
}
db.collection("portfolio", function(err, collection) {
collection.find().sort({order_num: 1}).toArray(function(err, result) {
var portfolio = [];
if (err) {
throw err;
} else {
for (i=0; i<result.length; i++) {
portfolio[i] = result[i];
}
res.render('index.html', {portfolio: portfolio});
}
});
});
// Here's the additional collection call:
db.collection("about", function(err, collection) {
collection.find().sort({order_num: 1}).toArray(function(err, result) {
var about = [];
if (err) {
throw err;
} else {
for (i=0; i<result.length; i++) {
about[i] = result[i];
}
res.render('index.html', {about: about});
}
});
});
});
});
这是控制台中的错误:
ReferenceError: /Volumes/Files/WebDev/RHC/michael/michael/views/index.html:62
60| <div class="row-fluid">
61| <ul id="work" class="thumbnails">
>> 62| <% for(i=0; i<portfolio.length; i++) { %>
63| <% if (portfolio[i].proj_type == "work") { %>
64| <% var newLine = ""; %>
65| <% var fancyBox = ""; %>
portfolio is not defined
因此,在MongoDB中,数据库下有两个集合,称为"michael",我试图在一个MongoClient.connect()下调用这两个集合.然后,我将结果通过两个数组(通过EJS)发送到前端:组合"和关于".看来,当我这样做时,它将使投资组合"变得不确定.希望有道理.
So I have two collections in MongoDB under the db called "michael", and I'm trying to call both of them under one MongoClient.connect(). I'm then sending the results to the front-end (via EJS) in two arrays: "portfolio" and "about". It seems that when I do this, it renders "portfolio" undefined. Hope that makes sense.
推荐答案
好吧,使用aesede的解决方案以及我自己的修补方法,我得到了它来渲染两个集合.它可能需要一些技巧,但这是我得到的:
Ok, using aesede's solution plus my own tinkering, I got it to render two collections. It could probably use some finessing but here's what I got:
// GLOBAL ARRAYS FOR STORING COLLECTION DATA
var collectionOne = [];
var collectionTwo = [];
app.get('/', function(req, res){
MongoClient.connect("mongodb://localhost:27017/michael", function(err, db) {
if(!err) {
console.log("We are connected");
}
db.collection("collectionOne", function(err, collection) {
collection.find().sort({order_num: 1}).toArray(function(err, result) {
if (err) {
throw err;
} else {
for (i=0; i<result.length; i++) {
collectionOne[i] = result[i];
}
}
});
db.collection("collectionTwo", function(err, collection) {
collection.find().sort({order_num: 1}).toArray(function(err, result) {
if (err) {
throw err;
} else {
for (i=0; i<result.length; i++) {
collectionTwo[i] = result[i];
}
}
});
});
// Thank you aesede!
res.render('index.html', {
collectionOne: collectionOne,
collectionTwo: collectionTwo
});
});
});
});
我发现的唯一错误是,当Node重新启动并在浏览器中单击刷新"时,我没有看到HTML呈现的任何内容.但是,随后进行的任何刷新都会始终显示内容.
The only bug, per se, that I found, was that when Node restarted and I hit "refresh" in the browser, I didn't see any content being rendered in the HTML. However, any subsequent refresh showed the content consistently.
这篇关于Node.JS,Express&MongoDB ::多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!