问题描述
在创建带有嵌套对象(例如对象数组)的文档时,每个对象都具有自己的_id.例如,我的架构如下所示:
When creating a document with nested objects (e.g. an array of objects), each object is given its own _id. For example, my schema looks like this:
mongoose = require "mongoose"
Schema = mongoose.Schema
schema = new Schema
name:
type: String
required: true
unique: true
trim: true
lists: [
list:
type: Schema.Types.ObjectId
required: true
ref: "List"
allocations: [
allocation:
type: Number
required: true
]
]
createdAt:
type: Date
default: Date.now
updatedAt:
type: Date
# Ensure virtual fields are serialised.
schema.set "toJSON",
virtuals: true
exports = module.exports = mongoose.model "Portfolio", schema
在最终创建文档时,lists
数组中的每个对象都被赋予_id,而lists.allocations
数组中的每个allocation
对象也被赋予_id.这似乎有些矫kill过正,并且使文档blo肿,但是MongoDB(或Mongoose)需要文档包含此附加信息是有原因的吗?如果没有,我想防止它发生,以便唯一的_id位于根文档上.
Every object in the lists
array is given an _id, as is every allocation
object in the lists.allocations
array, when documents are eventually created. This seems like overkill and bloats the document, but is there a reason MongoDB (or Mongoose) needs the document to contain this additional information? If not, I'd like to prevent it from happening so that the only _id is on the root document.
此外,猫鼬会自动为_id
创建一个虚拟id
,这是我需要的,因为我的客户端代码需要一个字段id
.这就是为什么我要用JSON返回虚拟机的原因.但是,由于整个文档中都有_id
个字段,而不仅仅是在根目录中,因此此虚拟副本重复了 all 个.如果无法阻止其他_id字段,如何获得仅应用于根文档_id的虚拟?或者,如果有更好的方法来做我要尝试做的事,那会是什么?
Furthermore, Mongoose automatically creates a virtual id
for _id
, which I need because my client code expects a field id
. This is why I'm having virtuals returned with JSON. However, because there are _id
fields all throughout the document, not just at the root, this virtual duplicates all of them. If there is no way to prevent the additional _id fields, how can I get a virtual to only apply only to the root document _id? Or if there is a better way to do what I'm trying to do with it, what would it be?
推荐答案
我已经找到了一种使用相同技术解决这两个问题的方法:通过为每种嵌套对象类型使用显式架构并设置其_id
和 false
的选项.看起来,当嵌套定义内联"的对象时,Mongoose为幕后的每个对象创建架构.由于架构的默认值为_id: true
和id: true
,因此它们将获得_id
并具有虚拟的id
.但是通过使用显式架构覆盖它,我可以控制_id
的创建.更多代码,但是我得到了我想要的:
I have figured out a way to solve both issues with the same technique: by using explicit schemas for each nested object type and setting their _id
and id
options to false
. It seems that when nesting objects that you define "inline", Mongoose creates schemas for each of those objects behind the scenes. Since the default for a schema is _id: true
and id: true
, they will get an _id
as well as have a virtual id
. But by overriding this with an explicit schema, I can control the _id
creation. More code, but I get what I want:
mongoose = require "mongoose"
Schema = mongoose.Schema
AllocationSchema = new Schema
allocation:
type: Number
required: true
,
_id: false
id: false
mongoose.model "Allocation", AllocationSchema
ListsSchema = new Schema
list:
type: Schema.Types.ObjectId
required: true
ref: "List"
allocations: [AllocationSchema]
,
_id: false
id: false
mongoose.model "Lists", ListsSchema
PortfolioSchema = new Schema
name:
type: String
required: true
unique: true
trim: true
lists: [ListsSchema]
createdAt:
type: Date
default: Date.now
updatedAt:
type: Date
这篇关于猫鼬在所有嵌套对象中添加_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!