简体   繁体   English

在Meteor中删除Mongo数据库集合

[英]Dropping a Mongo Database Collection in Meteor

Is there any way to drop a Mongo Database Collection from within the server side JavaScript code with Meteor? 有没有办法从Meteor的服务器端JavaScript代码中删除Mongo数据库集合? (really drop the whole thing, not just Meteor.Collection.remove({}); it's contents) (真的放弃了整个事情,而不仅仅是Meteor.Collection.remove({});它的内容)

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? 另外,还有一种方法可以从服务器端JavaScript代码中删除Meteor.Collection而不删除相应的数据库集合吗?

Why do that? 为什么这样?

  • Searching in the subdocuments (subdocuments of the user-document, eg userdoc.mailbox[12345] ) with underscore or similar turns out quiet slow (eg for large mailboxes). 使用下划线或类似搜索子文档(用户文档的子文档,例如userdoc.mailbox[12345] )会变得安静缓慢(例如,对于大邮箱)。
  • 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. Mongo文档还有大小限制,因此如果我将用户的所有消息存储在他/她的用户文档中,则邮箱的最大大小与所有其他用户数据一起小于16 MB。

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. 所以我想为我的每个用户创建一个数据库,将其用作邮箱,然后一条消息的最大大小为16 MB(非常可接受),我可以使用mongo查询搜索邮箱。

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). 此外,因为我正在使用Meteor,所以每当用户登录时,将这个mongo db集合作为Meteor.Collection加载将是很好的。当用户停用他/她的帐户时,db当然应该被删除,如果用户只需注销,只应删除Meteor.Collection(并在他/她再次登录时恢复)。

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. 在某种程度上,我已经有了这个工作,每个用户都有一个自己的邮箱数据库,但如果有人取消他/她的帐户,我必须手动删除这个特定的Mongo集合。 Also, I have do keep all mongo db collections alive as Meteor.Collections at all times because I cannot drop them. 此外,我已经将所有mongo db集合保持为Meteor.Collections,因为我无法删除它们。

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);
});

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. 因此,如果客户端操作会话变量“mailboxFilter”,则更新订阅并且用户在minimongo中获取新的消息。

It works very nice, the only thing missing is db collection dropping. 它工作得非常好,唯一缺少的是db collection drop。

Thanks for any hint already! 谢谢你的任何暗示!

*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();

This is better in my opinion because Meteor could randomly drop or rename the private method without any warning. 这在我看来更好,因为Meteor可以在没有任何警告的情况下随机丢弃或重命名私有方法。

You can completely drop the collection myMailbox with myMailbox._dropCollection() , directly from meteor. 您可以使用myMailbox._dropCollection()直接从meteor中删除myMailbox集合。

I know the question is old, but it was the first hit when I searched for how to do this 我知道这个问题很老,但是当我搜索如何做到这一点时,这是第一次打击

Searching in the subdocuments... 在子文档中搜索...

Why use subdocuments? 为什么要使用子文档? A document per user I suppose? 我认为每个用户的文档?

each message must be it's own document 每条消息必须是它自己的文档

That's a better way, a collection of messages, each is id'ed to the user. 这是一种更好的方式,一组消息,每一个都是用户的身份。 That way, you can filter what a user sees when doing publish subscribe. 这样,您可以过滤用户在进行发布订阅时看到的内容。

dropping all messages in one db turns out to be very slow for many users with large mailboxes 对于具有大邮箱的许多用户而言,丢弃一个数据库中的所有消息对于非常慢是非常慢的

That's because most NoSQL DBs (if not all) are geared towards read-intensive operations and not much with write-intensive. 这是因为大多数NoSQL DB(如果不是全部)都面向读取密集型操作,而写入密集型则不多。 So writing (updating, inserting, removing, wiping ) will take more time. 因此,写入(更新,插入,删除, 擦除 )将花费更多时间。

Also, some online services (I think it was Twitter or Yahoo) will tell you when deactivating the account: "Your data will be deleted within the next N days." 此外,一些在线服务(我认为是Twitter或雅虎)会在停用帐户时告诉您:“您的数据将在接下来的N天内被删除。” or something that resembles that. 或类似的东西。 One reason is that your data takes time to delete. 一个原因是您的数据需要时间删除。

The user is leaving anyway, so you can just tell the user that your account has been deactivated, and your data will be deleted from our databases in the following days. 用户无论如何都要离开,因此您可以告诉用户您的帐户已停用,并且您的数据将在接下来的几天内从我们的数据库中删除。 To add to that, so you can respond to the user immediately, do the remove operation asynchronously by sending it a blank callback. 要添加它,以便您可以立即响应用户,通过向其发送空白回调异步执行删除操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM