Mongoose不会在数据库中创建第二个集合

5ktev3wc  于 2023-03-03  发布在  Go
关注(0)|答案(1)|浏览(102)

这里我正在使用mongoose在数据库中创建第二种类型的集合,第一个集合在数据库中创建,但第二个没有显示。我已经刷新了几次
但未显示在数据库中

const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/fruitsDB",{useNewUrlParser: true}) 

const fruitSchema = new mongoose.Schema({  // Creating a boiler plate how the data will be arranged inside the database
    name: String,
    rating: Number,
    review: String
});

const Fruit = mongoose.model("Fruit", fruitSchema) 
const fruit = new Fruit({
    name: "Apple",
    rating: 7,
    review: "5"
})
fruit.save();

// Challenge to create a people collection in the same Database

const peopleSchema = new mongoose.Schema({
    name: String,
    age: Number,
    education: String
})

const People = mongoose.model("People", peopleSchema);

const people = new People({
    name: "Kanha",
    age: 19,
    education: "Btech"
});

people.save()  // Some problem has happened and My data has not saved inside the Database

这是3t工作室的照片
enter image description here
我试过chatGpt,但它的解决方案不工作,它建议错误检测,但没有错误显示在控制台.

mklgxw1f

mklgxw1f1#

我已经将连接URL更改为mongoose.connect("mongodb://127.0.0.1/fruitsDB");,并且正确地将两个集合数据插入到mongo中。

const mongoose = require("mongoose");

mongoose.connect("mongodb://127.0.0.1/fruitsDB");

const fruitSchema = new mongoose.Schema({
  // Creating a boiler plate how the data will be arranged inside the database
  name: String,
  rating: Number,
  review: String,
});

const Fruit = mongoose.model("Fruit", fruitSchema);
const fruit = new Fruit({
  name: "Apple",
  rating: 7,
  review: "5",
});
fruit.save();

// Challenge to create a people collection in the same Database

const peopleSchema = new mongoose.Schema({
  name: String,
  age: Number,
  education: String,
});

const People = mongoose.model("People", peopleSchema);

const people = new People({
  name: "Kanha",
  age: 19,
  education: "Btech",
});

people.save();

相关问题