javascript - 将对象字面量数组传递给 Mongoose 模式之间的区别
问题描述
我只是想知道当从服务器接收 json 时,这段代码怎么会返回一个 _id 键。
const productSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please enter product name'],
trim: true,
maxLength: [100, 'Product name cannot exceed 100 characters'],
},
price: {
type: Number,
required: [true, 'Please enter product price'],
maxLength: [5, 'Product price cannot exceed 5 characters'],
default: 0.0,
},
description: {
type: String,
required: [true, 'Please enter product description'],
},
ratings: {
type: Number,
default: 0,
},
images: [
{
public_id: {
type: String,
required: true,
},
url: {
type: String,
required: true,
},
},
],
"images":
[
{
"_id": "6070a2351c2fe84ab8883e4c",
"public_id": "products/dsvbpny402gelwugv2le",
"url": "https://res.cloudinary.com/bookit/image/upload/v1608062030/products/dsvbpny402gelwugv2le.jpg"
}
],
我注意到从服务器请求时存在 _id 字段。这是自动的吗?
解决方案
您应该知道默认情况下,Mongoose 会在您的模式中添加一个 _id 属性。
您可以在这里了解更多信息:https ://mongoosejs.com/docs/guide.html#_id
推荐阅读
- javascript - 提高 JavaScript 中工厂模式的内存效率
- xml - 为从模板打开的每个文档设置 Office 功能区中的默认选项卡
- ibm-cloud - 无法在 IBM Cloud 中部署应用程序
- django - Django 迁移-“没有这样的表”
- netsuite - 如何在 Netsuite Suitescript 2.0 中按 CompanyId 搜索案例?
- javascript - AngularJS:循环单选按钮组
- powershell - 将文件移动到具有今天日期的新文件夹中
- ruby-on-rails - 如何将 Paperclip gem 与 Trestle gem 一起使用
- scala - 如何使用 scala 创建具有随机内容的大型 spark 数据框?
- android - 无法使用谷歌照片删除带有 ACTION_PICK 的图片