如果模式太动态,我应该使用mongoose吗?

时间:2021-12-25 18:39:21

What I have understood so far is that the mongoose needs us to define a schema. But what if my schema keeps changing on a per user basis. For instance, let's say there are thousands of users of mobile phones. Each user has a different kind of offer subscriptions and what nots. New offers keep coming, and he can even choose combos of offers, creating new offers on the fly. So these offers become keys holding sub documents of various other details regarding that offer. Such a schema can't be predefined. Shall I use mongoose then? Or stick to mongojs type thin-skin wrappers and forget about mongoose's ODM capabilities?

到目前为止,我所理解的是mongoose需要我们定义一个模式。但是如果我的模式在每个用户的基础上不断变化呢?例如,假设有成千上万的手机用户。每个用户都有不同类型的订阅服务。新的报价不断出现,他甚至可以选择组合的报价,创造新的动态报价。所以这些报价变成了密钥,包含了关于这个报价的各种细节的子文档。这样的模式不能预定义。那我用猫鼬吗?还是坚持使用mongojs类型的薄皮包装器,忘记mongoose的ODM功能?

2 个解决方案

#1


3  

You could create a Mixed type schema where there's no restriction on the type of the data you can store. However, it comes at a trade-off. Take a look at the official documentation for info and implementation details.

您可以创建一个混合型模式,其中对可以存储的数据类型没有限制。然而,这是一种交换。查看官方文档以获取信息和实现细节。

#2


0  

From the Mongoose docs:

猫鼬的文档:

NOTE: Any key/val set on the instance that does not exist in your schema is always ignored, regardless of schema option.

注意:模式中不存在的实例上的任何键/val集总是被忽略,不管模式选项是什么。

You can set the value when the model instance is created:

您可以在创建模型实例时设置值:

var task = new Task({'otherKey', 'some value'});

You could also put the ad-hoc values under a mixed subdocument type as well.

您还可以将临时值放在混合子文档类型下。

#1


3  

You could create a Mixed type schema where there's no restriction on the type of the data you can store. However, it comes at a trade-off. Take a look at the official documentation for info and implementation details.

您可以创建一个混合型模式,其中对可以存储的数据类型没有限制。然而,这是一种交换。查看官方文档以获取信息和实现细节。

#2


0  

From the Mongoose docs:

猫鼬的文档:

NOTE: Any key/val set on the instance that does not exist in your schema is always ignored, regardless of schema option.

注意:模式中不存在的实例上的任何键/val集总是被忽略,不管模式选项是什么。

You can set the value when the model instance is created:

您可以在创建模型实例时设置值:

var task = new Task({'otherKey', 'some value'});

You could also put the ad-hoc values under a mixed subdocument type as well.

您还可以将临时值放在混合子文档类型下。