NodeJS MongoDB 问题 save() 函数在集合中创建空文档
NodeJS MongoDB question save() function creating empty documents in collection
MongoDB 的新手,了解一些基本的 Node.按照 Node、Express 和 MongoDB 的在线教程进行操作。我有一些代码连接到远程集群并将文档推送到集合中。连接有效,但插入的文档是空的,因为它只包含自动生成的 ID。这是代码:
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log('DB Connection successful'));
const tourSchema = new mongoose.Schema();
({
name: {
type: String,
required: [true, 'A tour must have a name'],
unique: true,
},
rating: {
type: Number,
default: 4.5,
},
price: {
type: Number,
required: [true, 'A tour must have a price'],
},
});
const Tour = mongoose.model('Tour', tourSchema);
const testTour = new Tour({
name: 'aaaa',
rating: 3.0,
price: 397,
});
testTour
.save()
.then((doc) => {
console.log(doc);
})
.catch((err) => console.log('ERROR:', err));
这是输出:
如果我在 Compass 中查看,我可以看到创建的空文档,因此连接有效。实际连接字符串可以有一些不同的查询字符串参数吗?以下是 MongoDB 连接字符串的当前查询字符串参数(这些是默认值):retryWrites=true&w=majority
知道我在代码中可能遗漏了什么吗?
谢谢!
尝试改变这个:
const tourSchema = new mongoose.Schema();
({
至:
const tourSchema = new mongoose.Schema({
在第 13 行,您没有正确定义架构,并且其中没有定义任何实体,因为您在定义之前用 ;
关闭了架构。
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log('DB Connection successful'));
const tourSchema = new mongoose.Schema()
({
name: {
type: String,
required: [true, 'A tour must have a name'],
unique: true,
},
rating: {
type: Number,
default: 4.5,
},
price: {
type: Number,
required: [true, 'A tour must have a price'],
},
});
const Tour = mongoose.model('Tour', tourSchema);
const testTour = new Tour({
name: 'aaaa',
rating: 3.0,
price: 397,
});
testTour.save()
.then((doc) => {
console.log(doc);
})
.catch((err) => console.log('ERROR:', err));
MongoDB 的新手,了解一些基本的 Node.按照 Node、Express 和 MongoDB 的在线教程进行操作。我有一些代码连接到远程集群并将文档推送到集合中。连接有效,但插入的文档是空的,因为它只包含自动生成的 ID。这是代码:
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log('DB Connection successful'));
const tourSchema = new mongoose.Schema();
({
name: {
type: String,
required: [true, 'A tour must have a name'],
unique: true,
},
rating: {
type: Number,
default: 4.5,
},
price: {
type: Number,
required: [true, 'A tour must have a price'],
},
});
const Tour = mongoose.model('Tour', tourSchema);
const testTour = new Tour({
name: 'aaaa',
rating: 3.0,
price: 397,
});
testTour
.save()
.then((doc) => {
console.log(doc);
})
.catch((err) => console.log('ERROR:', err));
这是输出:
如果我在 Compass 中查看,我可以看到创建的空文档,因此连接有效。实际连接字符串可以有一些不同的查询字符串参数吗?以下是 MongoDB 连接字符串的当前查询字符串参数(这些是默认值):retryWrites=true&w=majority
知道我在代码中可能遗漏了什么吗?
谢谢!
尝试改变这个:
const tourSchema = new mongoose.Schema();
({
至:
const tourSchema = new mongoose.Schema({
在第 13 行,您没有正确定义架构,并且其中没有定义任何实体,因为您在定义之前用 ;
关闭了架构。
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log('DB Connection successful'));
const tourSchema = new mongoose.Schema()
({
name: {
type: String,
required: [true, 'A tour must have a name'],
unique: true,
},
rating: {
type: Number,
default: 4.5,
},
price: {
type: Number,
required: [true, 'A tour must have a price'],
},
});
const Tour = mongoose.model('Tour', tourSchema);
const testTour = new Tour({
name: 'aaaa',
rating: 3.0,
price: 397,
});
testTour.save()
.then((doc) => {
console.log(doc);
})
.catch((err) => console.log('ERROR:', err));