How to free up new Mongodb.Collection from Meteor project?


#1

I want to dynamically load the collection in meteor server when the user logs in, as shown below

Accounts.onLogin(function(user){
CurrentDB = new Mongo.Collection(user+"db");
}

The above code works well for the first time. When the second user logs in, the meteor is aware about both the first user collection and second user collection. For Example, When jack logs in first meteor loads the collection “jackdb” and when bill logs in under the same session, the meteor loads the collection “billdb” and still remembers the collection “jackdb”. Therefore I am trying to free up the Mongodb collection on user logout like shown below

Accounts.onLogout(function(user){
delete CurrentDB;
}

The above code just deletes the object reference but the project still remembers all the collection.

Is there any way to free up the collections from Meteor project during user logout or login??

I know ways to bypass the exceptions. I had escalated same problem before Create collection object dynamically

But all I want is to free up the collection. Thanks in advance


#2

You should be able to do this on the server (maybe use a method for this) using the underlying nodejs mongodb driver.

SomeCollection.rawCollection().drop(options);

Note that returns a Promise, so you may want to put it as an await inside an async function.

Alternatively, you can use wrapAsync if you want a more Meteoric behaviour:

SomeCollection.drop = Meteor.wrapAsync(SomeCollection.rawCollection().drop, SomeCollection.rawCollection());

// now drop this collection
SomeCollection.drop(options);

Documentation here


#3

@robfallows: Thanks for your timely response.

Actually I already tried using rawCollection.drop(). But that “deletes” the collection from the mongodb itself and when the user again logs in, it will throw the error that collection does not exists.

Therefore I just to want to “free up” the memory location of the collection object “from meteor project” and not delete the collection from the database.


#4

I’m still not entirely sure I understand what you’re trying to do here, but I see these two scenarios:

  1. You want to release the client-side data, but retain the collection and its data in the database.
  2. You want to release the client-side data and also the collection data in the database.

Scenario 1 would usually be addressed by pub/sub, but the strategy of one collection per user probably makes this difficult to achieve. You would basically need to unsubscribe to and data and wait for (or trust that) data will be removed before you try to switch collections.

Scenario 2 could be simulated with the drop() method discussed earlier, since the collection would be re-created when that user next signs in.


Having said all that, separate collections per user is unnecessary and over-complicates the architecture and code. You would find it much easier and no less secure if you have a single collection with a userId field to permit appropriate publication to the relevant user. That is how your requirement for per-user data is usually done.


#5

@robfallows @hwillson

This is what my exact scenario. There is an external script which constantly writes to the various mongodb collection all the time. But this external script has nothing to do with meteor application and nothing to do with meteor server code.

Then the collections from the mongodb are loaded into meteor server when the user logs in.

Accounts.onLogin(function(user){
CurrentDB = new Mongo.Collection(user+"db");
}

Once it is loaded it publish to the meteor client and the meteor client will subscribe to the collection and displays it to the user.

All I want is:

When the user logs out I want to unload the collection from the server and not touch the real collection that exists in the database.

Note: Since the external script writes to the database, I cannot use the userId field to permit the appropriate publication to the relevant user. Also I need to use multiple collections in my scenario