我正在尝试从 mongodb shell 查询我的数据库以检索所有条目。尽管如此,我的 find() 方法仍未返回任何内容。
这是我正在使用的 Mongoose 模型:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ArticleSchema = new Schema({
title: String,
content: String,
author: { type: String, default: 'Vlad'},
postDate: { type: Date, default: Date.now }
});
ArticleSchema.methods.formatTitle = function() {
var link = this.title.replace(/\s/g, '-');
return '/article/' + link;
};
ArticleSchema.methods.snapshot = function() {
var snapshot = this.content.substring(0, 500);
return snapshot;
};
module.exports = mongoose.model('Article', ArticleSchema);
在浏览器中加载我的 Express.js 应用程序时,我可以毫无问题地显示我使用 API 添加的所有文章。不过,我只想进入 mongo shell 并查询它们。我使用了以下查询:
// blog is the database name (mongodb://localhost:27017/blog)
db.blog.find()
我得到一个空字符串,基本上是控制台中的一个新行。
最佳答案
你的答案就在那里,在问题中:
// blog is the database name (mongodb://localhost:27017/blog)
db.blog.find()
所以 db
已经 指的是正确的数据库。代替 blog
,放置集合名称,而不是数据库名称。
db.articles.find()
关于node.js - MongoDB find() 什么都不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18913533/