I am building a cms where the site and pages are stored within mongo. I need to be able to create a new site using another site as a template.
For example:
Site A - news page and home page
Create a new site selecting site A as a template, this selects site A and then makes copies of each page site A owns and saves them under Site B
The result
Site A - news page and home page
Site B - news 开发者_开发技巧page and home page
Site B will be able to then use site A as a starting point to build there new site.
I am new to Mongo/mongoid so really would like some pointers about the best way to approach this.
Thanks
One way to organise the data is to use collections with the site ID as the namespace, giving you collections like:
site_a.pages
site_a.news
site_a.users
site_b.pages
site_b.news
site_b.users
To create a new site from an existing one, you would copy the relevant collections (possibly excluding .users
for example), changing the namespace. That is, copy site_a.pages
to site_c.pages
. In the shell:
db.site_a.pages.find().forEach( function(x){db.site_c.pages.insert(x)} );
Note that site_a
is not a database name, but the namespace of a collection.
There is a limit to the number of collections you can have in a database that might be a problem if you're hosting the CMS in the cloud for many customers. You could get around this by having different databases for different customers. You should read the documentation on this for more details.
Another way to do this is to put the site ID in each document and always filter for this in your queries. Then you would only have a few collections, for example:
pages
news
users
With the documents in those collections having a site_id
field:
{
"site_id": "site_a",
"page_title": "Homepage",
...
}
The copying would be similar to above (but with filters and updating the site_id
field in the function). I think namespaces for collections is a neater approach however.
精彩评论