【发布时间】:2018-01-18 01:41:53
【问题描述】:
在为集合创建架构并插入类型为 objectId(converting it to String) 的文档时,出现未定义类型错误。
const mongoose = require("mongoose");
let testSchema = new mongoose.Schema({
date: {type: Date, required: true},
test_id: {type: mongoose.Types.ObjectId().toString(), required: true},
}, {collection: 'timeslotsTest'});
let testModel = mongoose.model("test", testSchema);
timeslotModel.create({
"date":"2017/11/21",
"test_id":"1"
}
【问题讨论】:
-
不,你不能在这里做
ObjectId().toString()。如果你想要一个“字符串”,那么它是type: String。但是你真的应该让ObjectId保持原样。占用更少的空间,这样会更好。 -
谢谢。我是 mongoDB 的新手。好奇 - 是不是无法将 ObjectId 类型转换为字符串或者我的方式是错误的?