0

I'm currently implementing admin dashboard of online shopping app.I want to implement method to perform user deletion and store that deleted user data temporally on another collection.

(Copy_userdata->Save it on another collection -> delete original data)

As an example my users data currently available in collection called users, and after deleting that user particular user's data must be available in another collection, lets say deleted_users collection. Are there any easy way to do that? thanks!

Sohan
  • 6,252
  • 5
  • 35
  • 56
Ransaka Ravihara
  • 1,786
  • 1
  • 13
  • 30

4 Answers4

3

You will be modify some of the code but this is the basic logic,

Use aggregation for copy collections over Refer here for aggregate function using mongo client

So the function looks like this

public aggregation(collectionName: string, pipelines: Object[]): Promise<Array<any>>
    {
        return new Promise((resolve, reject) =>
        {
        let cursor: mongodb.AggregationCursor<any> = null;

             //Here you will use getCollection method on your own to fetch the collection
            this.getCollection(collectionName)
                .then((collection: mongodb.Collection) =>
                {
                    cursor = collection.aggregate(pipelines);

                    return cursor.toArray();
                })
                .then((result: Array<any>) =>
                {
                    return resolve(result);
                })
                .catch((error: any) =>
                { 
                    //error//
                });

    }

    public dropCollection(collectionName: string): Promise<any>
    {
        return new Promise((resolve, reject) =>
        {

           this.getCollection(collectionName)
                .then((collection: mongodb.Collection) =>
                {
                    collection.drop((err: Error, result: any) =>
                    {

                        if (err)
                        {
                            return reject(DataDropError);
                        }

                        return resolve(result);
                    });
                })
                .catch(reject);
        });
    }

     public async backupAndDrop()
    {
        const Object = [ { $match: {} }, { $out: "DeletedCollection" } ];

        try
        {
            await this.aggregationPipeline("originalCollection", Object);
            await  this.dropCollection("originalCollection");

        }
        catch (e)
        {
            throw e;
        }

    }

Also try to run this on your mongo shell:

db.originalCollection.aggregate([ { $match: {} }, { $out: "Backup" } ])
Sohan
  • 6,252
  • 5
  • 35
  • 56
0

Why don't you add a flag like isDeleted which is false by default and then make it true when the user is deleted?

Niraj Patel
  • 810
  • 8
  • 15
0

You can do something like this...

    Client.connect(connection_string, function(err, db) {
       if(err){
           console.log(err);
       }
       else{
           db.collection(CollectionA).find().forEach(function(d){ db.collection(CollectionB).insert(d); });
        }

Try out if it works.

This can help too: How to properly reuse connection to Mongodb across NodeJs application and modules

Thanny
  • 564
  • 5
  • 9
  • This will consume memory and it will be slow when you have millions of records in memory – Sohan Jan 10 '20 at 10:59
0

You can first find the record to be deleted and do a create with that data to the new collection and then delete the record.

db.collection(CollectionA).findOne({userIdTODelete}, function(err, res){ 
         db.collection(CollectionB).insertOne(res, function() { 
            db.collection(CollectionA).deleteOne({userIdTODelete}); 
    }) 
});