-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-db.js
More file actions
91 lines (76 loc) · 2.41 KB
/
Copy pathtest-db.js
File metadata and controls
91 lines (76 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 数据库连接测试脚本
const mongoose = require('mongoose');
// 数据库连接字符串
const MONGODB_URI = 'mongodb+srv://iceferryling:OA3sCSXh2LqrPCyv@cluster0.yfdbjdj.mongodb.net/?appName=Cluster0';
// 用户模型定义
const userSchema = new mongoose.Schema({
id: { type: String, required: true, unique: true },
username: { type: String, required: true },
name: { type: String, required: true },
image: String,
bio: String,
threads: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Thread'
}
],
onboarded: {
type: Boolean,
default: false
},
communities: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Community'
}
]
});
const User = mongoose.models.User || mongoose.model('User', userSchema);
// 测试函数
async function testDatabase() {
console.log('开始测试数据库连接和数据写入...');
try {
// 1. 连接数据库
console.log('步骤 1: 连接到 MongoDB 数据库');
await mongoose.connect(MONGODB_URI);
console.log('✓ 数据库连接成功');
// 2. 测试数据写入
console.log('\n步骤 2: 测试数据写入');
const testUser = {
id: 'test-user-123',
username: 'testuser',
name: 'Test User',
bio: 'This is a test user for demonstration purposes',
image: 'https://example.com/test-image.jpg'
};
console.log('创建测试用户数据:', testUser);
// 插入或更新用户
const result = await User.findOneAndUpdate(
{ id: testUser.id },
{
username: testUser.username.toLowerCase(),
name: testUser.name,
bio: testUser.bio,
image: testUser.image,
onboarded: true
},
{ upsert: true, new: true }
);
console.log('✓ 用户数据操作成功:', result);
// 3. 测试数据读取
console.log('\n步骤 3: 测试数据读取');
const foundUser = await User.findOne({ id: testUser.id });
console.log('✓ 查询到的用户数据:', foundUser);
// 4. 断开连接
console.log('\n步骤 4: 断开数据库连接');
await mongoose.disconnect();
console.log('✓ 数据库连接已断开');
console.log('\n🎉 所有测试步骤完成!数据库连接和数据写入功能正常。');
} catch (error) {
console.error('测试失败:', error.message);
await mongoose.disconnect();
}
}
// 执行测试
testDatabase();