d
This commit is contained in:
68
src/moogoose/demo.js
Normal file
68
src/moogoose/demo.js
Normal file
@@ -0,0 +1,68 @@
|
||||
// models/DiaryEntry.js
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const DiaryEntrySchema = new mongoose.Schema({
|
||||
userId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'User', // 关联用户(支持多用户)
|
||||
required: true
|
||||
},
|
||||
title: {
|
||||
type: String,
|
||||
trim: true,
|
||||
default: '无标题'
|
||||
},
|
||||
content: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true
|
||||
},
|
||||
date: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
index: true // 为日期建立索引,加速按日查询
|
||||
},
|
||||
tags: [{
|
||||
type: String,
|
||||
lowercase: true,
|
||||
trim: true
|
||||
}],
|
||||
mood: {
|
||||
type: Number,
|
||||
min: 1,
|
||||
max: 5,
|
||||
default: 3, // 1=非常差,5=非常好
|
||||
index: true
|
||||
},
|
||||
weather: {
|
||||
type: String,
|
||||
enum: ['晴', '阴', '雨', '雪', '雾', '其他'],
|
||||
default: '其他'
|
||||
},
|
||||
location: {
|
||||
lat: Number,
|
||||
lng: Number,
|
||||
address: String
|
||||
},
|
||||
media: [{
|
||||
type: String, // 图片/音频/视频 URL
|
||||
trim: true
|
||||
}],
|
||||
createdAt: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
},
|
||||
updatedAt: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
}
|
||||
}, {
|
||||
timestamps: true // 自动管理 createdAt / updatedAt
|
||||
});
|
||||
|
||||
// 为常用查询建立复合索引
|
||||
DiaryEntrySchema.index({ userId: 1, date: -1 }); // 按用户+日期降序(最新在前)
|
||||
DiaryEntrySchema.index({ userId: 1, tags: 1 }); // 按标签查找
|
||||
DiaryEntrySchema.index({ userId: 1, mood: 1 }); // 按情绪筛选
|
||||
|
||||
module.exports = mongoose.model('DiaryEntry', DiaryEntrySchema);
|
||||
Reference in New Issue
Block a user