[英]mongoose 'minimize' empty nested object
我創建了一個包含多級嵌套屬性的貓鼬模式。 是這里:
var Person = new mongoose.Schema({
name: String,
parents: {
mom: {
name: String,
birthYear: Number
},
dad: {
name: String,
birthYear: Number
}
}
});
在我的示例中,文檔狀態可能有 2 種情況:
parents
對象中設置parents
對象必須為空{}
我的問題是,如果parent
對象為空,則文檔存儲如下:
{
name: 'Some Buddy',
parents: {
mom: {},
dad: {}
}
}
雖然預期的數據應該是這樣的:
{
name: 'Some Buddy'
parents: {}
}
我已經閱讀了很多關於minimize: true
但它並沒有為我提供完整的解決方案,因為根本沒有保存parents
對象。 所以簡而言之,我想最小化每個空的嵌套屬性(頂級屬性除外)。
你知道我該如何解決這個問題嗎?
謝謝!
同樣的事情發生在我身上。 在下面的架構中,starred 未初始化
Schema({minimize: false})
export class User {
@Prop()
_id: Number;
@Prop()
name: string;
@Prop()
sessionId: string;
@Prop()
_token: string;
@Prop({type: mSchema.Types.Map})
starred: {
type: mSchema.Types.Map,
of: starred,
default: {}
};
}
export interface starred {
owner: string;
repo: Number;
tags: string[];
}
運行后的 MongoDB 輸出
{"_id":{"$numberInt":"id"},"name":"name","sessionId":"wPEOw6vL0oTHKKav2nSlRg==","_token":"token","__v":{"$numberInt":"0"}}
我的對象數組也是空的,盡管我排除了“最小化”選項:
我做了很多研究,但不知道如何解決這個問題
const mongoose = require('mongoose');
const DateSchema = { type: Date, required: false, default: new Date(false) };
const NumberSchema = { type: Number, required: false, default: 1 };
const StringSchema = { type: String, required: false, default: "" };
const CustomSchema = {
tempoaSchedaInOre: DateSchema,
tempoTotaleInOre: DateSchema
};
const fasiSchema = new mongoose.Schema( {
utente: StringSchema,
fase: [{
controlloMateriale : CustomSchema,
equipaggiamentog : CustomSchema
}]
}
,{ minimize: false } //Crea componenti anche se vuoti
);
mongoose.model('fasi',fasiSchema);
在 MongoDb 中生成:
{"_id":{"$oid":"5fddcffbada9d34460510774"},"utente":"","fase":[],"__v":0}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.