最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

通过 node.js 将数据插入 monogodb

网站源码admin26浏览0评论

通过 node.js 将数据插入 monogodb

通过 node.js 将数据插入 monogodb

我运行这个 app.js。问题是我无法添加集合(插入数据)。我应该怎么办? 看来我已经成功连接到服务器了

这是我的 app.js 代码。

const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
  // Use connect method to connect to the server
  await mongoose.connect('mongodb://127.0.0.1:27017/FruitsDB');

  const fruitSchema = new mongoose.Schema({
    name: String,
    rating: Number,
    review: String
  });

  const Fruit = mongoose.model('Fruit', fruitSchema);

  const fruit = new Fruit({
    name: 'Apple',
    rating: 7,
    review: 'Pretty solid as a fruit.'
  });

 // await fruit.save();

  const fruits = await Fruit.find();
  console.log(fruits);

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

  const Person = mongoose.model('Person', personSchema);

  const person = new Person({
    name: 'John',
    age: 37
  });

 // await person.save();

  const people = await Person.find();
  console.log(people);

  const kiwi = new Fruit({
    name: 'Kiwi',
    score: 10,
    review: 'The best fruit!'
  });

  const orange = new Fruit({
    name: 'Orange',
    score: 4,
    review: 'Too sour for me'
  });

  const banana = new Fruit({
    name: 'Banana',
    score: 3,
    review: 'Weird texture'
  });



  Fruit.insertMany([kiwi, orange, banana])
  .then(function () {
    console.log("Successfully saved defult items to DB");
  })
  .catch(function (err) {
    console.log(err);
  });

}

如下所示,FruitsDB 中没有数据。

顺便说一句,这是我从终端得到的。


FruitsProject % node app.js
[]
[]
Successfully saved defult items to DB

我尝试了不同类型的代码,但我仍然无法向数据库添加任何数据。

回答如下:

我认为问题在于您正在尝试将

fruit
person
文档插入
FruitsDB
数据库,但该数据库不存在。您需要先创建数据库,然后才能将文档插入其中。您可以通过将以下代码行添加到您的
main()
函数来执行此操作:

await mongoose.createCollection('FruitsDB');

创建数据库后,您应该能够将文档插入其中。

这里是完整的代码更改:

const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
  // Use connect method to connect to the server
  await mongoose.connect('mongodb://127.0.0.1:27017/');

  // Create the FruitsDB database
  await mongoose.createCollection('FruitsDB');

  const fruitSchema = new mongoose.Schema({
    name: String,
    rating: Number,
    review: String
  });

  const Fruit = mongoose.model('Fruit', fruitSchema);

  const fruit = new Fruit({
    name: 'Apple',
    rating: 7,
    review: 'Pretty solid as a fruit.'
  });

  await fruit.save();

  const fruits = await Fruit.find();
  console.log(fruits);

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

  const Person = mongoose.model('Person', personSchema);

  const person = new Person({
    name: 'John',
    age: 37
  });

  await person.save();

  const people = await Person.find();
  console.log(people);

  const kiwi = new Fruit({
    name: 'Kiwi',
    score: 10,
    review: 'The best fruit!'
  });

  const orange = new Fruit({
    name: 'Orange',
    score: 4,
    review: 'Too sour for me'
  });

  const banana = new Fruit({
    name: 'Banana',
    score: 3,
    review: 'Weird texture'
  });



  Fruit.insertMany([kiwi, orange, banana])
  .then(function () {
    console.log("Successfully saved defult items to DB");
  })
  .catch(function (err) {
    console.log(err);
  });

}
发布评论

评论列表(0)

  1. 暂无评论