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

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

问题描述

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

Normally, MongoDB Collections are defined like this:

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

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

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

建立Collection名称很容易:

It would be easy enough to build up the Collection name:

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.

实际上,您想要的是一组有限的集合,其中一些集合具有灵活的架构.正如安德鲁·毛(Andrew Mao)在此相关问题的答案中所指出的,

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天全站免登陆