sayanriju

DMT: Node 8+ Script to "clean" MongoDB for New Chats Feat

Jul 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const MongoClient = require('mongodb').MongoClient
  2.  
  3. const url = 'mongodb://localhost:27017/donatemytime';
  4.  
  5. MongoClient.connect(url, async function(err, db) {  
  6.   console.log("Connected successfully to server");
  7.  
  8.   const User = db.collection("users")
  9.   const TaskEvent = db.collection("taskevents")
  10.   const Project = db.collection("projects")
  11.   const Application = db.collection("applications")
  12.   const Activity = db.collection("activities")
  13.   const Conversation = db.collection("conversations")
  14.   const Message = db.collection("messages")
  15.  
  16.   await Application.deleteMany({})
  17.   await Conversation.deleteMany({})
  18.   await Message.deleteMany({})
  19.   await Activity.updateMany({}, {$set: {_appliedVolunteers: []}})
  20.  
  21.   try {
  22.     const docs = await User.find({ isNgo: true }).toArray()  
  23.     for (const doc of docs) {
  24.       const newConversation = {
  25.         context: "pool",
  26.         title: `${doc.ngoName}'s Volunteer Pool`,
  27.        _ngoId: doc._id,
  28.        _participants: [doc._id, ... doc.NGOPoolMembers] // NGO itself + volunteer pool members (more will be added dynamically)
  29.      }
  30.      const savedConversation = await Conversation.insertOne(newConversation)
  31.      const savedDoc = await User.updateOne({ _id: doc._id }, { $set: { _conversationId: savedConversation.insertedId } })
  32.    }
  33.  } catch (err) {
  34.    console.log("ERROR: : ", err)
  35.  }
  36.  
  37.  try {
  38.    const docs = await Project.find().toArray()  
  39.    for (const doc of docs) {
  40.      const newConversation = {
  41.      context: "project",
  42.      title: doc.title,
  43.      _projectId: doc._id,
  44.      _participants: [doc._addedBy] // only the NGO at first; volunteers invloved will be added when applications are approved
  45.    }
  46.      const savedConversation = await Conversation.insertOne(newConversation)
  47.      const savedDoc = await Project.updateOne({ _id: doc._id }, { $set: { _conversationId: savedConversation.insertedId } })
  48.    }
  49.  } catch (err) {
  50.    console.log("ERROR: : ", err)
  51.  }
  52.  
  53.  try {
  54.    const docs = await TaskEvent.find().toArray()  
  55.    for (const doc of docs) {
  56.      const newConversation = {
  57.      context: "taskevent",
  58.      title: doc.title,
  59.      _projectId: doc._projectId,
  60.      _taskEventId: doc._id,
  61.      isTask: doc.isTask,
  62.      _participants: [doc._addedBy] // only the NGO at first; volunteers invloved will be added when applications are approved
  63.    }
  64.      const savedConversation = await Conversation.insertOne(newConversation)
  65.      const savedDoc = await TaskEvent.updateOne({ _id: doc._id }, { $set: { _conversationId: savedConversation.insertedId } })
  66.    }
  67.  } catch (err) {
  68.    console.log("ERROR: : ", err)
  69.  }
  70.  
  71. console.log("--------------------- the end ------------------")
  72. db.close()
  73.  
  74. });
Add Comment
Please, Sign In to add comment