Mongoose 和 MongoDB - 似乎无法在我的 collection 中找到文档
Mongoose and MongoDB - Can't seem to find documents in my collection
标题说明了一切。我正在尝试学习 MongoDB 并使用 find()
函数来 select 我 Comments
collection.
中的所有文档
我正在尝试在我的 routes.js 文件中直接使用 Mongoose,如果有更清晰的约定,请告诉我,我是一般 NodeJS 的新手...
routes.js
var express = require("express"),
router = express.Router(),
mongoose = require("mongoose"),
Comment = require("../models/Comment");
router.get("/comments", function(req, res, next)
{
mongoose.set("debug", true);
mongoose.connect("mongodb://127.0.0.1/Comments");
var comments = [];
Comment.find(function(err, array)
{
var i = 0;
for(var o in array)
{
comments[i] = o;
i++;
console.log("Found comment!");
}
});
res.render("comments", {
comments: comments
});
mongoose.connection.close();
});
module.exports = router;
Comment.js(型号)
var mongoose = require("mongoose");
var CommentSchema = new mongoose.Schema({
author: String,
message: String,
posted: Date
});
// Name of the model, the schema, and the collection
module.exports = mongoose.model("Comment", CommentSchema, "Comments");
正如您在我的 routes.js 文件中看到的那样,我将调试设置为 true,因此每当我请求 /comments
资源时,我都会得到以下输出:
除了查询到达 Mongo 守护程序之外,我无法理解此输出,所以我猜我要么错误地导入了我的模型,要么我只是使用 Mongoose 不正确。我的 Comments
collection 填充了数据;我可以通过 Mongo CLI 连接并使用 db.Comments.find()
并显示文档。
为了不使问题膨胀,我省略了我的观点。谢谢大家的宝贵时间。
解决方案:
routes.js(固定)
var express = require("express"),
router = express.Router(),
mongoose = require("mongoose"),
Comment = require("../models/Comment");
mongoose.set("debug", true);
mongoose.connect("mongodb://127.0.0.1/Comments");
router.get("/comments", function(req, res, next)
{
Comment.find(function(err, dataset)
{
res.render("comments", {
comments: dataset
});
});
});
module.exports = router;
应该注意 mongoose.model.find()
是异步的,所以我不得不在 find()
回调中移动我的 Express 渲染操作,否则我的数据集变量将在它被填充之前被渲染。
Comment.js 没有错误,因此没有针对此解决方案进行更新。
问题是您在异步 Comment.find
调用完成之前调用 mongoose.connection.close()
。
删除 mongoose.connection.close()
调用并将 mongoose.connect
调用移至您应用的启动代码。只需在应用的整个生命周期内让创建的连接池保持打开状态,而不是在每次请求时打开和关闭它。
标题说明了一切。我正在尝试学习 MongoDB 并使用 find()
函数来 select 我 Comments
collection.
我正在尝试在我的 routes.js 文件中直接使用 Mongoose,如果有更清晰的约定,请告诉我,我是一般 NodeJS 的新手...
routes.js
var express = require("express"),
router = express.Router(),
mongoose = require("mongoose"),
Comment = require("../models/Comment");
router.get("/comments", function(req, res, next)
{
mongoose.set("debug", true);
mongoose.connect("mongodb://127.0.0.1/Comments");
var comments = [];
Comment.find(function(err, array)
{
var i = 0;
for(var o in array)
{
comments[i] = o;
i++;
console.log("Found comment!");
}
});
res.render("comments", {
comments: comments
});
mongoose.connection.close();
});
module.exports = router;
Comment.js(型号)
var mongoose = require("mongoose");
var CommentSchema = new mongoose.Schema({
author: String,
message: String,
posted: Date
});
// Name of the model, the schema, and the collection
module.exports = mongoose.model("Comment", CommentSchema, "Comments");
正如您在我的 routes.js 文件中看到的那样,我将调试设置为 true,因此每当我请求 /comments
资源时,我都会得到以下输出:
除了查询到达 Mongo 守护程序之外,我无法理解此输出,所以我猜我要么错误地导入了我的模型,要么我只是使用 Mongoose 不正确。我的 Comments
collection 填充了数据;我可以通过 Mongo CLI 连接并使用 db.Comments.find()
并显示文档。
为了不使问题膨胀,我省略了我的观点。谢谢大家的宝贵时间。
解决方案:
routes.js(固定)
var express = require("express"),
router = express.Router(),
mongoose = require("mongoose"),
Comment = require("../models/Comment");
mongoose.set("debug", true);
mongoose.connect("mongodb://127.0.0.1/Comments");
router.get("/comments", function(req, res, next)
{
Comment.find(function(err, dataset)
{
res.render("comments", {
comments: dataset
});
});
});
module.exports = router;
应该注意 mongoose.model.find()
是异步的,所以我不得不在 find()
回调中移动我的 Express 渲染操作,否则我的数据集变量将在它被填充之前被渲染。
Comment.js 没有错误,因此没有针对此解决方案进行更新。
问题是您在异步 Comment.find
调用完成之前调用 mongoose.connection.close()
。
删除 mongoose.connection.close()
调用并将 mongoose.connect
调用移至您应用的启动代码。只需在应用的整个生命周期内让创建的连接池保持打开状态,而不是在每次请求时打开和关闭它。