猫鼬创建空数组? [英] Mongoose creating empty arrays?

查看:55
本文介绍了猫鼬创建空数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

var questionSchema = new schema({
  title: String,
  subtitle: String,
  required: Boolean,
  type: String,
  create_date: Date,
  question_id: Number,
  suvey_id: Number,
  items: Array
});
var question = mongoose.model("Question", questionSchema);
var quest = getMyQuestion();

var record = new question({
  title: quest.question,
  subtitle: quest.subtitle,
  required: quest.answer_required,
  type: quest.question_type,
  create_date: quest.create_date,
  question_id: quest.id,
  survey_id: quest.survey_id
});

record.save();

但是,当我从数据库中提取此记录时,它总是将items属性定义为一个空数组(而不是根本不存在).

But when I pull this record from my database it always has the items attribute defined as an empty array (rather than not being there at all).

猫鼬是故意这样做的吗?如果可以,为什么?尝试强制完全不定义属性(而不是将其定义为空数组)对我来说不是一个好主意吗?

Is mongoose doing this on purpose? If so why? Would it be a bad idea for me to try and force the attribute to not be defined at all (rather than being defined as an empty array)?

推荐答案

猫鼬是故意这样做的,尽管我不确定为什么.如果您将不想存储的属性设置为undefined,则这些属性将从文档中排除.

Mongoose does do this on purpose, though I'm not sure why. If you set the properties that you don't want stored as undefined, they will be excluded from the document.

使用猫鼬将mongo对象的字段设置为空

这篇关于猫鼬创建空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆