mongoose 为什么MongoDB不保存创建方法中没有提供的空值列?

bqujaahr  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(215)

我试图在集合中插入一些数据,我的模式看起来像下面提到的代码,但在执行创建操作时,我无法看到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中的所有列?
所有这些列都在哪里?至少它应该用空值保存。

yjghlzjz

yjghlzjz1#

如果你来自SQL的背景,将collection类比为table,将path/field类比为column有助于轻松理解,但有时也会造成混乱。
SQL表中,对于在插入过程中没有指定任何值的列,将在中设置默认值(null)。对于mongodb文档,这不是真的,因为它只是二进制JSON。在JSON中,除非显式指定任何字段为null,否则该字段将不存在于JSON对象中。
MongoDB是无模式的,但mongoose是一个 Package 器,它抽象了这一点,给人的印象是在mongoDB文档上有一个模式。现在mongooseSchema是一个地方,你可以定义验证/约束/等vanilla,通常伴随着有一个严格的模式。
要使用mongooseModel.create方法进行插入,需要为每个path/field指定一个default值,如下所示:

const UserSchema = new mongoose.Schema({
  firstName: {type: String, required: true},
  lastName: {type: String, required: true},
  fbImage: {type: String, required: false, default: ''},
  address: { type: Address, required: false, default: null }
});

const Address = new mongoose.Schema({
  street: {type: String, required: false},
  city: {type: String, required: false},
  country: {type: String, required: false}
});

相关问题