我正在使用 Express 和 MongoDB 开发 NodeJS 应用程序。我的对象没有保存到数据库中,所以我深入挖掘,发现如果我使用以下方法,则没有创建目标集合:
const PersonSchema = new Schema({
firstName: String,
});
如果我使用了该集合,则创建该集合
const PersonSchema = new Schema({
firstName: { type: String, unique: true},
});
在任何一种情况下,完整的模型server/models/person.js都是:
'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Use native promises.
mongoose.Promise = global.Promise;
// Define the Person schema.
const PersonSchema = new Schema({
// ...
});
const Person = mongoose.model('Person', PersonSchema);
module.exports = Person;
我有 MongoDB 4.2.3(db.version()在 mongo shell 中)、npm 6.14.4、mongoose 5.7.5 和 npm mongodb 3.3.2(在 mongo shell 中npm list | grep mongodb)。
为什么MongoDB除了_id创建集合之外还需要一个唯一的字段?
慕桂英4014372
相关分类