Skip to content

Instantly share code, notes, and snippets.

@syuilo
Last active January 18, 2016 09:16
Show Gist options
  • Save syuilo/1c45a23a7586baf33f2e to your computer and use it in GitHub Desktop.
Save syuilo/1c45a23a7586baf33f2e to your computer and use it in GitHub Desktop.
// DELETE UNUSE FIELD
db.Users.update({}, {$unset: {"latestStatusText": 1}}, false, true);
// INIT latestPost FIELDS
db.Users.update({}, {$set: {"latestPost": null}}, false, true);
// INIT prevPost FIELDS
db.Posts.update({}, {$set: {"prevPost": null}}, false, true);
// INIT nextPost FIELDS
db.Posts.update({}, {$set: {"nextPost": null}}, false, true);
// ALL USER
db.Users.find().forEach(function(user) {
// SET LATEST POST
db.Posts
.find({user: user._id})
.sort({cursor: -1})
.limit(1)
.forEach(function(latest) {
db.Users.update({_id: user._id}, {
$set : {"latestPost": latest._id}
}, false, false);
});
// ALL USER'S POSTS
db.Posts.find({user: user._id}).sort({cursor: 1}).forEach(function(post) {
// print(user.screenName + " : " + post.cursor);
// Next
db.Posts
.find({
user: user._id,
cursor: {$gt: post.cursor}
})
.sort({createdAt: 1})
.limit(1)
.forEach(function(next) {
if (next != null) {
db.Posts.update({_id: post._id}, {
$set : {"nextPost": next._id}
}, false, false);
} else {
db.Posts.update({_id: post._id}, {
$set : {"nextPost": null}
}, false, false);
}
});
// Prev
db.Posts
.find({
user: user._id,
cursor: {$lt: post.cursor}
})
.sort({createdAt: -1})
.limit(1)
.forEach(function(prev) {
if (prev != null) {
db.Posts.update({_id: post._id}, {
$set : {"prevPost": prev._id}
}, false, false);
} else {
db.Posts.update({_id: post._id}, {
$set : {"prevPost": null}
}, false, false);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment