我试图在集合中插入一些数据,我的模式看起来像下面提到的代码,但在执行创建操作时,我无法看到DB中所有的列,至少不能看到空值。
知名度架构.js
const mongoose = require("mongoose");
const CelebritySchema = new mongoose.Schema({
born: {type: String, required: true},
height: {type: String, required: false},
connections: {
father: {type: String, required: false},
mother: {type: String, required: false},
sister: {type: String, required: false},
brother: {type: String, required: false},
wife: {type: String, required: false},
husband: {type: String, required: false},
children: {
son: {type: String, required: false},
daughter: {type: String, required: false}
}
},
profile: {
name: {type: String, required: false},
picture: {type: String, required: false},
alt: {type: String, required: false},
fullName: {type: String, required: false}
},
pictures: {
sideBar: {
image: {type: String, required: false},
alt: {type: String, required: false}
}
},
details: [
{
title: {type: String, required: false},
data: {type: String, required: false}
},
],
type: {type: String, required: false},
occupation: {type: String, required: false},
placeOfBirth: {type: String, required: false},
nationality: {type: String, required: false},
school: {type: String, required: false},
college: {type: String, required: false},
qualification: {type: String, required: false},
martialStatus: {type: String, required: false},
path: {type: String, required: false},
username: {type: String, required: false},
category: {type: String, required: false, default: 'celebrity'},
seo: {
title: {type: String, required: false},
description: {type: String, required: false}
},
socialMedia: {
fb: {type: String, required: false},
twitter: {type: String, required: false},
insta: {type: String, required: false},
linkedIn: {type: String, required: false}
},
honours: [{type: String, required: false}],
awards: [
{type: String, required: false}
],
aboutMe: [{type: String, required: false}],
biography: [
{
title: {type: String, required: false},
description: [
{type: String, required: false}],
image: {type: String, required: false}, alt: {type: String, required: false}
},
],
latestNews: [{type: String, required: false}],
table: [{
title: String,
label: [String],
data: [Object]
}]
}, {timestamps: true, toObject: {virtuals: true, getters: true}});
const Celebrity = mongoose.model('Celebrity', CelebritySchema, 'Celebrity');
module.exports = Celebrity
我现在尝试使用下面的代码在名人表中插入一些数据。
Celebrity.create({
title:"title",
born:"today",
placeOfBirth:"USA"
}).then(r=> console.log(r))
将其保存到数据库中后,当我尝试签入数据库时,只发现了几列,如下图所示
在这段代码中我是否遗漏了什么,为什么在保存数据后我不能看到DB中的所有列?
所有这些列都在哪里?至少它应该用空值保存。
1条答案
按热度按时间yjghlzjz1#
如果你来自
SQL
的背景,将collection
类比为table
,将path
/field
类比为column
有助于轻松理解,但有时也会造成混乱。在
SQL
表中,对于在插入过程中没有指定任何值的列,将在中设置默认值(null)。对于mongodb文档,这不是真的,因为它只是二进制JSON
。在JSON
中,除非显式指定任何字段为null
,否则该字段将不存在于JSON
对象中。MongoDB是无模式的,但mongoose是一个 Package 器,它抽象了这一点,给人的印象是在mongoDB文档上有一个模式。现在
mongoose
的Schema
是一个地方,你可以定义验证/约束/等vanilla,通常伴随着有一个严格的模式。要使用
mongoose
的Model.create
方法进行插入,需要为每个path/field
指定一个default
值,如下所示: