简体   繁体   English

mongoose自定义模式类型

[英]mongoose custom schema types

I read from mongoose documentation that it is possible to create custom schema types to be added to the ones already existing. 我从mongoose文档中读到,可以创建自定义模式类型以添加​​到已存在的模式类型中。

As suggested I tried to look into the mongoose-long example: https://github.com/aheckmann/mongoose-long 正如我所建议的那样,我试着研究一下mongoose-long的例子: https//github.com/aheckmann/mongoose-long

I need this in an application I am developing in which I have a profile that is a mongoose model. 我需要在我开发的应用程序中使用它,其中我有一个mongoose模型的配置文件。 The profile has several fields such as name , surname and so on that are MultiValueField . 该配置文件有多个字段,如namesurname等,它们是MultiValueField A MultiValueField is on object with the following structure: MultiValueField位于对象上,具有以下结构:

{
    current : <string>
    providers : <Array>
    values : {
         provider1 : value1,
         provider2 : value2
    }
}

The structure above has a list of allowed providers (ordered by priorities) from which the field's value can be retrieved. 上面的结构有一个允许的提供者列表(按优先级排序),从中可以检索字段的值。 The current property keeps track of which provider is currently picked to use as value of the field. 当前属性会跟踪当前选择哪个提供程序作为字段的值。 Finally the object values contains the values for each provider. 最后,对象值包含每个提供程序的值。

I have defined an object in node.js, whose constructor takes the structure above as argument and provides a lot of useful methods to set a new providers, adding values and so on. 我在node.js中定义了一个对象,其构造函数将上面的结构作为参数,并提供了许多有用的方法来设置新的提供者,添加值等等。

Each field of my profile should be initialized with a different list of providers, but I haven't found a way yet to do so. 我的个人资料的每个字段都应该使用不同的提供者列表进行初始化,但我还没有找到方法。

If I define MultiValueField as an Embedded Schema , I can do so only having each field as Array . 如果我将MultiValueField定义为Embedded Schema ,我只能将每个字段作为Array Also I cannot initialize every field at the moment a profile is created with the list of providers. 此外,在使用提供者列表创建配置文件时,我无法初始化每个字段。

I think the best solution is to define a SchemaType MultiValueFieldType , that has a cast function that returns a MultiValueField object . 我认为最好的解决方案是定义一个SchemaType MultiValueFieldType ,它具有一个返回MultiValueField objectMultiValueField object However, how can I define such a custom schema type? 但是,如何定义这样的自定义架构类型? And how can I use custom options when defining it in the schema of the profile? 在配置文件的架构中定义时,如何使用自定义选项?

I already posted a question on mongoose Google group to ask how to create Custom Schema Types: https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/fCpxtKSXFKc 我已经在mongoose Google网上发布了一个问题,询问如何创建自定义架构类型: https//groups.google.com/forum/? fromgroups#! topic / mongoose -orm / fCpxtKSXFKc

I was able to create custom schema types only as arrays too. 我也能够仅将自定义模式类型创建为数组。 You will be able to initialize the model, but this aproach has drawbacks: 您将能够初始化模型,但这种方法有缺点:

  • You will have arrays even if you don't need them 即使您不需要它们,也会有阵列
  • If you use ParameterSchema on other model, you will need to replicate it 如果在其他模型上使用ParameterSchema,则需要复制它

Custom Schema Type Code: 自定义架构类型代码:

// Location has inputs and outputs, which are Parameters

// file at /models/Location.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ParameterSchema = new Schema({
  name: String,
  type: String,
  value: String,
});

var LocationSchema = new Schema({
    inputs: [ParameterSchema],
    outputs: [ParameterSchema],
});

module.exports = mongoose.model('Location', LocationSchema);

To initialize the model: 要初始化模型:

// file at /routes/locations.js
var Location = require('../models/Location');

var location = { ... } // javascript object usual initialization

location = new Location(location);
location.save();

When this code runs, it will save the initialized location with its parameters. 当此代码运行时,它将使用其参数保存初始化位置。

I didn't understand all topics of your question(s), but I hope that this answer helps you. 我不明白你问题的所有主题,但我希望这个答案可以帮助你。

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

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