Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var userName = req.body.userName
- connection.connectedusers.updateMany({}, { $push: { connectArray: [userName] } }, { upsert: true }, function (err, docs) {
- if (err) {
- console.log(err);
- } else {
- connection.connectedusers.find({}, async (err, list) => {
- if (err) {
- console.log(err);
- }
- else {
- setTimeout(async () => {
- if (list[0].connectArray.length >= 2) {
- clearTimeout();
- var shortListed = list[0].connectArray.slice(0, 2);
- console.log(shortListed,'shortlisted array')
- try {
- await connection.connectedusers.update({},
- { $pull: { connectArray: { $in: shortListed } } },
- { multi: true }
- )
- const docs = await connection.connectedusers.find({});
- res.json({
- message: 'users selected successfully',
- status: 1,
- docs: docs
- });
- } catch (err) {
- res.status(201).json(err);
- }
- } else {
- const docs = await connection.connectedusers.find({});
- console.log(docs, 'docs')
- const allUsers = docs.connectArray;
- console.log(allUsers, 'allUsers')
- await connection.connectedusers.update({},
- { $pull: { connectArray: { $in: allUsers } } },
- { multi: true }
- )
- res.json({
- message: 'users not selected',
- status: 2,
- docs: docs,
- allUsers: allUsers
- })
- }
- }, 10000);
- }
- });
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement