使用 Mongoose 动态创建集合 [英] Dynamically create collection with Mongoose

查看:21
本文介绍了使用 Mongoose 动态创建集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户能够在我的 Node 应用程序中创建集合.我真的只见过猫鼬集合中硬编码的例子.任何人都知道是否可以使用猫鼬动态创建集合?如果是这样,一个例子会很有帮助.

I want to give users the ability to create collections in my Node app. I have really only seen example of hard coding in collections with mongoose. Anyone know if its possible to create collections dynamically with mongoose? If so an example would be very helpful.

基本上我希望能够在不同集合中存储不同事件"的数据.

Basically I want to be able to store data for different 'events' in different collections.

I.E.事件:事件1,事件2,...事件N

I.E. Events: event1, event2, ... eventN

用户可以在那里创建自己的自定义事件并将数据存储在该集合中.最后,每个事件可能有数百/数千行.我想让用户能够对他们的事件执行 CRUD 操作.我想将每个事件数据存储在不同的集合中,而不是存储在一个大集合中.

Users can create there own custom event and store data in that collection. In the end each event might have hundreds/thousands of rows. I would like to give users the ability to perform CRUD operations on their events. Rather than store in one big collection I would like to store each events data in a different collection.

我没有真正尝试过的例子,因为我只用猫鼬创建了硬编码"集合.我什至不确定我是否可以根据用户请求在 mongoose 中创建一个动态的新集合.

I don't really have an example of what I have tried as I have only created 'hard coded' collections with mongoose. I am not even sure I can create a new collection in mongoose that is dynamic based on a user request.

var mongoose = require('mongoose');
mongoose.connect('localhost', 'events');

var schema = mongoose.Schema({ name: 'string' });
var Event1 = mongoose.model('Event1', schema);

var event1= new Event1({ name: 'something' });
event1.save(function (err) {
  if (err) // ...
  console.log('meow');
});

如果我将Event1"硬编码为一个集合,则上述方法效果很好.不确定我是否创建了动态集合.

Above works great if I hard code 'Event1' as a collection. Not sure I create a dynamic collection.

var mongoose = require('mongoose');
mongoose.connect('localhost', 'events');

...

var userDefinedEvent = //get this from a client side request
...

var schema = mongoose.Schema({ name: 'string' });
var userDefinedEvent = mongoose.model(userDefinedEvent, schema);

你能做到吗?

推荐答案

来自这里的 mongo 文档:数据建模

From mongo docs here: data modeling

在某些情况下,您可能会选择将信息存储在多个集合而不是单个集合.

In certain situations, you might choose to store information in several collections rather than in a single collection.

考虑存储日志文档的示例收集日志各种环境和应用.日志集合包含以下形式的文件:

Consider a sample collection logs that stores log documents for various environment and applications. The logs collection contains documents of the following form:

{ log: "dev", ts: ..., info: ... } { log: "debug", ts: ..., info: ...}

{ log: "dev", ts: ..., info: ... } { log: "debug", ts: ..., info: ...}

如果文档总数较少,您可以将文档分组为按类型收集.对于日志,考虑维护不同的日志集合,例如 logs.dev 和 logs.debug.logs.dev 集合将仅包含与开发环境相关的文档.

If the total number of documents is low you may group documents into collection by type. For logs, consider maintaining distinct log collections, such as logs.dev and logs.debug. The logs.dev collection would contain only the documents related to the dev environment.

一般来说,拥有大量的集合没有显着的性能损失并导致非常好的性能.清楚的集合对于高吞吐量批处理非常重要.

Generally, having large number of collections has no significant performance penalty and results in very good performance. Distinct collections are very important for high-throughput batch processing.

这篇关于使用 Mongoose 动态创建集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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