在Meteor中删除Mongo数据库集合 [英] Dropping a Mongo Database Collection in Meteor
问题描述
有没有办法从Meteor的服务器端JavaScript代码中删除Mongo数据库集合? (真的放弃了整个事情,不只是 Meteor.Collection.remove({});
它的内容)
Is there any way to drop a Mongo Database Collection from within the server side JavaScript code with Meteor? (really drop the whole thing, not just Meteor.Collection.remove({});
it's contents)
另外,还有一种方法可以从服务器端JavaScript代码中删除Meteor.Collection而不删除相应的数据库集合吗?
In addition, is there also a way to drop a Meteor.Collection from within the server side JavaScript code without dropping the corresponding database collection?
为什么这样做?
- 在子文档中搜索(用户文档的子文档,例如
userdoc.mailbox [12345]
)使用下划线或类似结果变得安静缓慢(例如对于大邮箱)。 - 另一方面,将所有用户的所有邮件(在邮箱示例的上下文中)放在一个中大数据库,然后搜索*一个或多个特定邮件的所有邮件变得非常非常慢(对于许多具有大邮箱的用户)。
- 还有大小限制对于Mongo文档,因此如果我将用户的所有消息存储在他/她的用户文档中,则邮箱的最大大小为< 16 MB以及所有其他用户数据。
- Searching in the subdocuments (subdocuments of the user-document, e.g.
userdoc.mailbox[12345]
) with underscore or similar turns out quiet slow (e.g. for large mailboxes). - On the other hand, putting all messages (in context of the mailbox-example) of all users in one big DB and then searching* all messages for one or more particular messages turns out to be very, very slow (for many users with large mailboxes), too.
- There is also the size limit for Mongo documents, so if I store all messages of a user in his/her user-document, the mailbox's maximum size is < 16 MB together with all other user-data.
所以我希望为每个用户建立一个数据库,以便将其用作邮箱,然后一条消息的最大大小是16 MB(非常可接受),我可以使用mongo查询搜索邮箱。
So I want to have a database for each of my user to use it as a mailbox, then the maximum size for one message is 16 MB (very acceptable) and I can search a mailbox using mongo queries.
此外,因为我正在使用Meteor,每当用户登录时,将mongo db集合作为Meteor.Collection加载会很好。当用户停用他/她的帐户时,db当然应该被删除,如果用户刚刚注销,只有Meteor .Collection应该被删除(并在他/她再次登录时恢复)。
Furthemore, since I'm using Meteor, it would be nice to then have this mongo db collection be loaded as Meteor.Collection whenever a user logs in. When a user deactivates his/her account, the db should of course be dropped, if the user just logs out, only the Meteor.Collection should be dropped (and restored when he/she logs in again).
在某种程度上,我已经完成了这项工作,每个用户都拥有一个自己的数据库邮箱,但如果有人取消他/她的帐户,我必须手动删除这个特定的Mongo集合。此外,我确实将所有mongo db集合保持为Meteor.Collections,因为我无法删除它们。
To some extent, I got this working already, each user has a own db for the mailbox, but if anybody cancels his/her account, I have to delete this particular Mongo Collection manually. Also, I have do keep all mongo db collections alive as Meteor.Collections at all times because I cannot drop them.
这是一个运行良好的服务器端代码片段每个用户邮箱一个集合:
This is a well working server-side code snippet for one-collection-per-user mailboxes:
var mailboxes = {};
Meteor.users.find({}, {fields: {_id: 1}}).forEach(function(user) {
mailboxes[user._id] = new Meteor.Collection("Mailbox_" + user._id);
});
Meteor.publish("myMailbox", function(_query,_options) {
if (this.userId) {
return mailboxes[this.userId].find(_query, _options);
};
});
而客户只是使用这段客户代码订阅某个查询:
while a client just subscribes with a certain query with this piece of client-code:
myMailbox = new Meteor.Collection("Mailbox_"+Meteor.userId());
Deps.autorun(function(){
var filter=Session.get("mailboxFilter");
if(_.isObject(filter) && filter.query && filter.options)
Meteor.subscribe("myMailbox",filter.query,filter.options);
});
因此,如果客户端操作会话变量mailboxFilter,则更新订阅并且用户获得在minimongo中的新一堆消息。
So if a client manipulates the session variable "mailboxFilter", the subscription is updated and the user gets a new bunch of messages in the minimongo.
它非常好用,唯一缺少的是db collection drop。
It works very nice, the only thing missing is db collection dropping.
感谢您提供任何暗示!
*我在这里写了丢弃,这是一个完全错误。我的意思是搜索。
*I previeously wrote "dropping" here, which was a total mistake. I meant searching.
推荐答案
不使用私有方法的解决方案是:
A solution that doesn't use a private method is:
myMailbox.rawCollection().drop();
我认为这更好,因为Meteor可以随意删除或重命名私有方法而不会发出任何警告。
This is better in my opinion because Meteor could randomly drop or rename the private method without any warning.
这篇关于在Meteor中删除Mongo数据库集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!