简体   繁体   English

使用 Mongo/Mongoose,为什么在将文档添加到现有集合时会创建一个全新的数据库?

[英]Using Mongo/Mongoose, why is an entirely new database created when adding a document to an existing collection?

https://i.imgur.com/w5quRwA.jpg https://i.imgur.com/w5quRwA.jpg

I manually created a database called "shoppingitems" on the mongodb website console.我在 mongodb 网站控制台上手动创建了一个名为“shoppingitems”的数据库。 I then created a model called "products" in an Express app and connected to the database.然后我在 Express 应用程序中创建了一个名为“products”的 model 并连接到数据库。 A collection called "products" was added to the "shoppingitems" database like I expected.如我所料,一个名为“products”的集合被添加到“shoppingitems”数据库中。

I then went to add a document to the "shoppingitems.products" collection, but instead an entirely new database called "test" was created, with a products collection and my submitted document in that 'test.products" collection instead of the "shoppingitems.products" collection like I intended.然后我将一个文档添加到“shoppingitems.products”集合中,但创建了一个名为“test”的全新数据库,其中包含一个产品集合和我提交的文档在“test.products”集合中而不是“shoppingitems” .products" 集合,就像我想要的那样。


Is there something wrong with my code?我的代码有问题吗? I make no mention of a "test" database anywhere, so IDK why it was created in the first place.我在任何地方都没有提到“测试”数据库,所以 IDK 为什么首先创建它。

index.js索引.js

//Express
var express = require("express");
const app = express();
app.use(express.json());

//Mongoose
const dotenv = require("dotenv");
dotenv.config();
const mongoose = require("mongoose");

mongoose
  .connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => console.log("db connection succesfull"))
  .catch((err) => console.log(err));

//CORS
const cors = require("cors");
app.use(cors());

//Routes
const productRoute = require("./routes/products");
app.use("/", productRoute);


//RUN INDEX.JS    
app.listen(5000, () => {
  console.log("backend server is running");
});

routes/products.js路线/products.js

var express = require("express");
var router = express.Router();
var Product = require("../models/Products");

/* GET PRODUCTS FOR HOMEPAGE */
router.get("/", async (req, res) => {
  try {
    productList = await Product.find();
    res.json(productList);
  } catch (error) {
    console.log(error);
  }
});

//POST PRODUCTS TO DATABASE

router.post("/", async (request, response) => {
  console.log("request.body= ", request.body);
  const newProduct = new Product(request.body);
  try {
    const savedProduct = await newProduct.save();
    response.status(201).json(savedProduct);
  } catch (err) {
    response.status(500).json(err);
  }
});

module.exports = router;

models/Products.js模型/Products.js

const mongoose = require("mongoose");

const ProductSchema = new mongoose.Schema({
  name: { type: String },
  price: { type: Number },
  description: { type: String },
  image: { type: String },
  stripeId: { type: String },
});

module.exports = mongoose.model("Product", ProductSchema);

Am I missing something?我错过了什么吗? I don't see anything in the code that would be causing this and creating a "test" database.我在代码中看不到任何会导致此问题并创建“测试”数据库的内容。 I've only used Mongo once or twice before though so I'm not exactly an expert.不过,我之前只用过一两次 Mongo,所以我算不上专家。 Can anybody here see what I'm doing wrong?这里有人能看到我做错了什么吗?

I'll post any additional code or information that you think is necessary to solving this.我将发布您认为解决此问题所必需的任何其他代码或信息。 Just tell me what else you need to see.告诉我你还需要看什么。

test is the default database name used if you don't specify one. test是默认的数据库名称,如果你没有指定的话。 I also notice that nowhere in the code is a shoppingsitems database mentioned.我还注意到代码中没有任何地方提到了shoppingsitems数据库。

The connection string could contain the database name, but in this code that is taken from an environment variable. 连接字符串可以包含数据库名称,但在这段代码中是从环境变量中获取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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