由于我的工作,我正在学习爬行。但是突然我开始了后端区域。 (Node.js,Express,MongoDB,GraphQL)我对MonoDB中的数据更新有疑问。
我已经用“猫鼬”连接到“ mLab”。而且我已经成功连接了虚拟数据。
schema.js
const Book = require("../model/book");
// dummy data
const books = [
{name: "Name of the Wind", genre: "Fantasy"},
{name: "The Final Empire", genre: "Fantasy"},
{name: "The Long Earth", genre: "Sci-Fi"},
{name: "The Hero of Ages", genre: "Fantasy"},
{name: "The Colour of Magic", genre: "Fantasy"},
{name: "The Light Fantastic", genre: "Fantasy"},
{name: "드래곤볼", genre: "Sci-Fi"}
];
Book.collection.insert(books, function (err, docs) {
if (err){
return console.error(err);
} else {
console.log("Multiple documents inserted to Collection");
}
});
book.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bookSchema = new Schema({
name: String,
genre: String
});
module.exports = mongoose.model("Book", bookSchema);
但是,当我在虚拟数据中添加新数据时,新数据将添加到现有数据之后。
如果我将初始值发送到空数据库,对数据进行更改时如何更新现有数据?