Skip to content

Question about switch_collection #1098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
muhammedabad opened this issue Aug 13, 2015 · 8 comments
Open

Question about switch_collection #1098

muhammedabad opened this issue Aug 13, 2015 · 8 comments

Comments

@muhammedabad
Copy link

Hi,

I'm quite new to mongodb and mongoengine and particular. We're looking at developing a multi tenanted django application that uses a single database, while giving each client their own collections for each of our models, eg:

client_1_products
client_1_orders

client_2_products
client_2_orders

What we would like to be able to do is easily change the collection name based on the client that using the app (each client would be assigned a collection prefix on registration). I have looked at the switch_collection() function, but I'm not sure if it's the best way to go about this. The site will be used by hundreds of people at the same time, so from a performance perspective I need to ensure that this does not impact negatively.

@touilleMan
Copy link
Member

Hi,

As you guessed, you can use the context_manager to temporaly switch collection.

Considering the performances, you can have a look at the implementation (https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/context_managers.py), it's basically a set/reset of the Document's configuration, so it must be pretty cheap.

@samuelfekete
Copy link

Given that switch_collection is not thread safe #788, if multiple clients use it at the same time, this could result in data being written to the wrong collection.

@wangyushun
Copy link

Ran into the same issue today,i know switch_collection is not thread safe now ,but I still need it ,what can be done to avoid this problem?

@samuelfekete
Copy link

@wangyushun,
My approach was to stop using switch_collection and instead dynamically generate one class per collection using inheritance, and simply call the correct class by name.

See here for more details.

@wangyushun
Copy link

@samuelfekete Thanks,Your solution is perfect, but I'm not sure if it fits me.I have a Bucket table. I don't know how big the table will be in the future. Each bucket has its own collection. Dynamically create a separate class for each bucket, the dict will be large. besides, I need to remove separate class from or insert into the dict dynamically when user create or delete a bucket. I don't know if this will lead to other problems. I found PyMODM, and I'm learning about it. But now I have no other solution, I'll try your solution first. Thanks again for your reply.

@samuelfekete
Copy link

@wangyushun, if you think the number of collections will be very large, you could also consider creating the classes only when you need them.

@commissarster
Copy link

@samuelfekete Your solution is very good. The solution should submit to mongoengine and solved the bug.

@focusj
Copy link

focusj commented May 17, 2019

Any progress on this issue @MongoEngine team?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants