NodeJS Mongo - Mongoose - 动态集合名称 [英] NodeJS Mongo - Mongoose - Dynamic collection name

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

问题描述

所以,我想创建一个基于客户端的分区模式,我将集合名称设置为 function(),我的伪代码是这样的:

So, I want to create a client side based paritioning schema, where I set the collection name as function(), my pseudo code is something like that:

var mongoose = require('mongoose'),
  Schema = mongoose.Schema,

var ConvForUserSchema = new Schema({
  user_id: Number,
  conv_hash: String,
  archived: Boolean,
  unread: Boolean
}, function CollectionName() {
  return (this.user_id % 10000);
});

这是否可以通过 Moongose 以任何方式实现,以便读取和写入都按预期工作?

Is this in any way possible through moongose such that both read and writes will work as expected?

推荐答案

集合名称逻辑在整个 Moongose 代码库中都是硬编码的,因此就目前情况而言,客户端分区是不可能的.

Collection name logic is hard coded all over the Moongose codebase such that client side partitioning is just not possible as things stands now.

我的解决方案是直接使用 mongo 驱动程序 -

My solution was to work directly with the mongo driver -

https://github.com/mongodb/node-mongodb-native

事实证明这很棒,与驱动程序直接合作的灵活性允许所有需要的东西,而且 Moongose 开销似乎在任何情况下都没有增加太多.

This proved great, the flexibility working with the driver directly allows for everything required and the Moongose overhead does not seem to add much in any case.

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

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