Created
September 12, 2018 13:26
-
-
Save akaDPR/d015c04d1b0caac6a3b70b8afc8c8d7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var http = require('http') | |
var mongojs = require('mongojs'); | |
var ObjectID = mongojs.ObjectID; | |
var db = mongojs(process.env.MONGO_URL || 'mongodb://localhost:27017/local'); | |
var archeive = mongojs(process.env.MONGO_URL || 'mongodb://localhost:27017/admin'); | |
var app = express(); | |
var server = http.Server(app); | |
const chatLimit = 5; | |
server.listen(3004, () => console.log('listening on *:3004')); | |
db.collection('messages').aggregate( | |
[{ | |
$group: { | |
_id: '$chatId' | |
} | |
}] | |
).toArray(function(err, data) { | |
data.forEach(function(item) { | |
db.collection('messages').find({ | |
chatId: item._id | |
}, function(err, messages) { | |
if (err) return res.status(500).send('Internal Server Error'); | |
var chatCount = messages.length; | |
if (chatCount > chatLimit) { | |
db.collection('messages').find({ | |
chatId: item._id | |
}).limit(5, function(err, rdata) { | |
rdata.forEach(function(rdataO) { | |
archeive.collection('messages').insert(rdataO, function() { | |
db.collection('messages').remove(rdataO); | |
}); | |
}) | |
}); | |
} | |
}); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment