如果您有子文档数组,Mongoose 会自动为每个子文档创建 ID。例如:
{
_id: "mainId"
subDocArray: [
{
_id: "unwantedId",
field: "value"
},
{
_id: "unwantedId",
field: "value"
}
]
}
有没有办法告诉Mongoose不要为数组中的对象创建id?
如果您有子文档数组,Mongoose 会自动为每个子文档创建 ID。例如:
{
_id: "mainId"
subDocArray: [
{
_id: "unwantedId",
field: "value"
},
{
_id: "unwantedId",
field: "value"
}
]
}
有没有办法告诉Mongoose不要为数组中的对象创建id?
很简单,你可以在子架构中定义它:
var mongoose = require("mongoose");
var subSchema = mongoose.Schema({
// your subschema content
}, { _id : false });
var schema = mongoose.Schema({
// schema content
subSchemaCollection : [subSchema]
});
var model = mongoose.model('tablename', schema);
_id
的子文档。只需在子文档声明中添加 _id: false
即可。var schema = new mongoose.Schema({
field1: {
type: String
},
subdocArray: [{
_id: false,
field: { type: String }
}]
});
这将防止在您的子文档中创建一个_id
字段。
在Mongoose v5.9.10
中测试过
此外,如果您使用对象文字语法指定子模式,也可以只添加_id: false
来禁止它。
{
sub: {
property1: String,
property2: String,
_id: false
}
}
我正在使用mongoose 4.6.3,我所需要做的就是在模式中添加_id:false,不需要创建子模式。
{
_id: ObjectId
subDocArray: [
{
_id: false,
field: "String"
}
]
}
_id
是一个字段而不是一个约束。 - jemiloii_id = mongoose.Types.ObjectId()
进行赋值,那么该_id是否会在整个集合中唯一? - Saitamavar subSchema = mongoose.Schema({
//subschema fields
},{ _id : false });
var subSchema = mongoose.Schema({
//subschema content
_id : false
});
在使用第二个选项之前,请检查您的mongoose版本。
@Schema({_id: false})
export class MySubDocument {
@Prop()
id: string;
}
id
和_id
的一些额外信息:/**
* Mongoose assigns each of your schemas an id virtual getter by default which returns the document's _id field
* cast to a string, or in the case of ObjectIds, its hexString.
*/
id?: boolean;
/**
* Mongoose assigns each of your schemas an _id field by default if one is not passed into the Schema
* constructor. The type assigned is an ObjectId to coincide with MongoDB's default behavior. If you
* don't want an _id added to your schema at all, you may disable it using this option.
*/
_id?: boolean;
如果你想将一个带有_id的预定义模式作为子文档(没有_id)使用,理论上可以按照以下方式进行:
const sourceSchema = mongoose.Schema({
key : value
})
const subSourceSchema = sourceSchema.clone().set('_id',false);
但那对我不起作用。所以我加了这个:
delete subSourceSchema.paths._id;
现在我可以在父文档中包含 subSourceSchema 而不需要 _id 了。 我不确定这是否是正确的做法,但它能工作。
_id
字段吗?还是只在将子模式用作子文档项数组嵌入的情况下才会跳过?我之所以问这个问题,是因为我今天在 Stack Overflow 上提出了类似的问题。 - Web User{ _id : true }
吗? - throrin19let studentSchema = new Schema({ studentId: { type: ObjectId, ref: Student.modelName }, performance: [performanceSchema] }, { _id: false });
到这个:let studentSchema = new Schema({ _id: false, id: false, studentId: { type: ObjectId, ref: Student.modelName }, performance: [performanceSchema] });
这样做停止了在studentSchema
上创建_id
,但保留了子文档performance
数组中对象的_id
创建。不确定是否需要同时使用_id: false
和id: false
。 - Web User