是否可以生成动态命名的 MongoDB 集合? [英] Is it possible to generate dynamically-named MongoDB Collections?

查看:17
本文介绍了是否可以生成动态命名的 MongoDB 集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,MongoDB 集合的定义如下:

Normally, MongoDB Collections are defined like this:

DuckbilledPlatypi = new Mongo.Collection("duckbilledplatypi"); 

不过,我想根据用户输入动态生成集合.例如,我可能希望它是:

I want to, though, dynamically generate Collections based on user input. For example, I might want it to be:

RupertPupkin20151212_20151218 = new Mongo.Collection("rupertPupkin20151212_20151218"); 

建立集合名称很容易:

var dynCollName = username + begindate +'_'+ enddate;

...然后将dynCollName")传递给 Mongo.Collection:

...and then pass "dynCollName") to Mongo.Collection:

 = new Mongo.Collection(dynCollName); 

...但是Collection 实例名称怎么样——它是如何动态生成的?我需要类似的东西:

...but what about the Collection instance name - how can that be dynamically generated? I would need something like:

"RupertPupkin20151212_20151218".ToRawName() = new Mongo.Collection(dynCollName);

-或:

"RupertPupkin20151212_20151218".Unstringify() = new Mongo.Collection(dynCollName);

...但是AFAIK,没有这样的东西...

...but AFAIK, there's no such thing...

推荐答案

在单个客户端实例上,是的,您可以动态引用它.但是在一般情况下(使用它在服务器和所有连接的客户端之间同步数据),不需要.

On a single client instance, yes, and you could dynamically reference it. However in the general case (using it to sync data between the server and all connected clients), no.

我在常见错误动态创建的集合部分解决了这一点 稍微详细一点,但基本问题是让所有连接的客户端就动态生成的集合集达成一致将非常复杂.

I address this point in the Dynamically created collections section of common mistakes in a little detail, but the fundamental problem is that it would be highly complex to get all connected clients to agree on a dynamically generated set of collections.

更有可能的是,一组有限的集合(其中一些集合具有灵活的架构)实际上是您想要的.正如安德鲁毛在回答这个相关问题时指出的那样,partitioner 是另一种可用于帮助解决导致此问题的情况的工具.

It's much more likely that a finite set of collections where some have a flexible schema, is actually what you want. As Andrew Mao points out in the answer to this related question, partitioner is another tool available to help address some cases which give rise to this question.

这篇关于是否可以生成动态命名的 MongoDB 集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆