該賞金過期4天。此問題的答案有資格獲得 100聲望獎勵。 Romik romikromik正在從信譽良好的來源尋找答案。
我閱讀了官方檔案,但仍然不明白https://docs.mongodb.com/manual/tutorial/model-embedded-one-to-one-relationships-between-documents/ 請 解釋一下,如何在我的例子。我有帶有貓鼬的 nest.js 應用程式。兩個模式(資料庫中有 2 個表):
1)
export type UserModelDocument = UserModel & Document;
@Schema()
export class UserModel {
@Prop()
email: string;
@Prop({ type: MongooseSchema.Types.ObjectId, ref: 'InviteModel' })
invite: InviteModel;
}
export const UserSchema = SchemaFactory.createForClass(UserModel);
@Schema()
export class InviteModel {
@Prop()
status: string;
}
export const InviteSchema = SchemaFactory.createForClass(InviteModel);
在這里,在我的服務中,我創建了一個用戶:
async create(createUserDto: CreateUserDto) {
const userExists = await this.userModel.findOne({ email: createUserDto.email });
if (userExists) {
this.logger.error('User already exists');
throw new NotAcceptableException('User already exists');
}
const createdUser = await new this.userModel(createUserDto).save();
//need some operations to save default invite too in the same user
return toCreateUserDto(createdUser);
}
如何在注冊新用戶時一對一添加關系,使創建的用戶物件也添加邀請物件。例子:
{userEmail: "[email protected]",
invite: {
status: 'not_sent',
}
}
我可以在一個請求中完成嗎?或者我需要保存在不同的請求中?
const createdUser = await new this.userModel(createUserDto).save();
const createdInvite = await new this.inviteModel(createInviteDto).save(); ?
uj5u.com熱心網友回復:
檔案中有很好的解釋,如果您搜索的資料不那么頻繁,您可以將其卸載到第二個集合以減小主檔案的大小并提高讀取性能,如果您需要不那么頻繁的資料,您將再次請求第二個集合。一般來說,如果您的第一個集合中的檔案不是那么大并且對讀取性能沒有影響,最好是將該欄位嵌入第一個集合中并避免太多集合并在單個呼叫中獲取所有內容。
- 一對一示例:
集合 1:
{ _id:"useremail" , frequentData: "the data1" }
集合2:
{ _id:"useremail" , lessFrequentData:"the data2" }
- 嵌入選項:
集合 1:
{ _id:"usermail" , frequentData: "the Data1" , lessFrequentData:"the Data2" }
PS我的個人觀察:mongoDBwiredTiger存盤引擎正在將資料拆分并存盤到32KB的存盤塊中,因此低于該大小的檔案被認為相對較小,如果沒有其他特定要求,嵌入似乎是最佳選擇......檔案大小很重要,但在檔案模型設計中首選的是非規范化資料,正確的索引創建和利用將有助于更多的性能提升。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367579.html
下一篇:檢查檔案或目錄是否被隱藏
