-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
mongo_migration_forum.js
99 lines (92 loc) · 2.41 KB
/
mongo_migration_forum.js
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
92
93
94
95
96
97
98
99
print("Hashing users")
var userHash = {};
db.user2.find({},{oid:1}).forEach(function(user) {
userHash[user.oid.toString()] = user._id;
});
function user(oid) {
if(userHash[oid]) return userHash[oid];
throw "Missing user " + oid;
}
var categSlugs = {};
var topicIds = {};
(function(oldColl, coll) {
print("Categs");
var cursor = oldColl.find(), nb = 0;
coll.drop();
while(cursor.hasNext()) {
var obj = cursor.next();
categSlugs[obj._id] = obj.slug;
coll.insert({
_id: obj.slug,
pos: obj.position,
name: obj.name,
desc: obj.description
});
nb ++;
}
coll.ensureIndex({pos: 1}, {unique: true});
print("Done categs: " + nb);
})(db.forum_category, db.f_categ);
(function(oldColl, coll) {
print("Topics");
var cursor = oldColl.find(), nb = 0;
coll.drop();
while(cursor.hasNext()) {
var obj = cursor.next();
var id = makeId(8);
topicIds[obj._id] = id;
var categId = categSlugs[obj.category["$id"]];
if (categId == null) {
print("Skip topic without categ: " + obj.subject);
} else {
coll.insert({
_id: id,
slug: obj.slug,
categId: categId,
createdAt: obj.createdAt,
updatedAt: obj.pulledAt,
views: obj.numViews,
name: obj.subject
});
}
nb ++;
}
coll.ensureIndex({categId: 1, slug: 1}, {unique: true});
coll.ensureIndex({categId: 1});
coll.ensureIndex({categId: 1, updatedAt: -1});
print("Done topics: " + nb);
})(db.forum_topic, db.f_topic);
(function(oldColl, coll) {
print("Posts");
var cursor = oldColl.find(), nb = 0;
coll.drop();
while(cursor.hasNext()) {
var obj = cursor.next();
var topicId = topicIds[obj.topic["$id"]];
if (topicId != null) {
var post = {
_id: makeId(8),
topicId: topicId,
createdAt: obj.createdAt,
author: obj.authorName || "Anonymous",
text: obj.message,
number: obj.number
};
if (obj.author) {
post.userId = user(obj.author['$id'].toString());
}
coll.insert(post);
}
nb ++;
}
coll.ensureIndex({topicId: 1});
coll.ensureIndex({topicId: 1, createdAt: 1});
print("Done posts: " + nb);
})(db.forum_post, db.f_post);
function makeId(size) {
var text = "";
var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < size; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}