无法使用expressjs mongoose库从本地MongoDB读取数据。

huangapple go评论113阅读模式
英文:

Can not read data from local MongoDB using expressjs mongoose library

问题

我正在学习Mongoose和MongoDB,并尝试从本地托管的MongoDB中读取数据,我从Mongoose文档中输入了类似的示例。但它不起作用。
我创建了一个简单的MongoDB集合,名为blogs,位于名为personal_website的数据库中,用于测试目的。这个集合中只有一个文档。

personal_website> db.blogs.find()
[ { _id: ObjectId("63bbcb27ae437ded486fecf4"), title: 'test' } ]

以下是我的代码,它不起作用

user@user-machine: ~$ cat server.js

const mongoose = require("mongoose");
mongoose.set("strictQuery", true);
mongoose.connect("mongodb://localhost/personal_website", () => {
	console.log("connected to databse personal_website");
});

let blogSchema = new mongoose.Schema({
  title: String,
});

let Blog = mongoose.model("blog", blogSchema);

let blogCount = Blog.find({}).count();
console.log(blogCount);

我运行node server.js,但我得到了这个输出:

Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
  _executionStack: null,
  .........
  connected to databse personal_website

我期望得到与在MongoDB shell中运行db.blogs.find().count()命令相似的结果。我该如何修复这个问题?我应该在连接建立后查询数据库吗?我看到很多教程都像我一样在一开始就连接到他们的MongoDB,而且文档上也说“Mongoose允许您立即开始使用您的模型,无需等待mongoose与MongoDB建立连接”。所以你们可以帮我找出这个问题的原因以及如何修复吗?我非常感激,我卡在这里已经四个小时了。

英文:

I am learning Mongoose and MongoDB and try to read data from a local hosted MongoDB, I typed similar example from Mongoose documentation. But it doesn't work.
I create a simple MongoDB collection named blogs inside a database called personal_website for test purposes. And there is only one document in this collection

personal_website> db.blogs.find()
[ { _id: ObjectId("63bbcb27ae437ded486fecf4"), title: 'test' } ]

There is my code which doesn't work

user@user-machine: ~$ cat server.js

const mongoose = require("mongoose");
mongoose.set("strictQuery", true);
mongoose.connect("mongodb://localhost/personal_website", () => {
	console.log("connected to databse personal_website");
});

let blogSchema = new mongoose.Schema({
  title: String,
});

let Blog = mongoose.model("blog", blogSchema);

let blogCount = Blog.find({}).count();
console.log(blogCount);

I run node server.js but I got this output:

Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
  _executionStack: null,
.........
connected to databse personal_website

I expect get the similar result as i run db.blogs.find().count() command in MongoDB shell. How could I fix that? Should I query database after the connection established? I saw a lot of tutorials connect to their MongoDB at the very beginning just like me and also the documentation says "Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB". So can you guys help me figure out what cause this problem and how to fix it? I really appreciate that, I stuck there like four hours.

答案1

得分: 1

  1. 在你的连接字符串中使用端口号和 127.0.0.1
    "mongodb://127.0.0.1:27017/personal_website",而不是 "mongodb://localhost/personal_website"(27017 是默认端口)。

  2. 尝试将你的查询包装在异步函数中,并使用 await:

async function count() {
  let blogCount = await Blog.find({}).count();
  console.log(blogCount);
}

count();
英文:
  1. use port number and 127.0.0.1 in your connection string:
    "mongodb://127.0.0.1:27017/personal_website" instead of "mongodb://localhost/personal_website" (27017 is default port)

  2. try to wrap your query with async function and use await:

async function count() {
  let blogCount = await Blog.find({}).count();
  console.log(blogCount);
}

count();

huangapple
  • 本文由 发表于 2023年1月9日 17:57:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定