根据精益查询的结果创建猫鼬模型 [英] Create mongoose model from results of lean query
问题描述
我正在使用精益查询来更快地从数据库中检索大型数据集.检索到的大多数数据都是使用的,但未修改或保存,因此精益查询很好,但是我需要使用架构方法来修改和保存一小部分对象.
I am using a lean query to retrieve a large dataset from my database more quickly. Most of the data retrieved is used but not modified or saved, so the lean query is fine, but I need to modify and save a small proportion of the objects using the schema methods.
在遍历精益查询的结果时是否可以为这些结果创建猫鼬对象,而大多数结果都以精益格式保留?
Is it possible to create the mongoose objects for these results as I iterate through the results of my lean query, leaving the majority of the results in the lean format?
我知道的唯一方法是使用数据创建新对象,然后将isNew
设置为false,但这需要我为此目的构造一个构造函数,而且似乎有些怪异.所以我想知道toObject()
The only way I know of is to create new objects using the data, then set isNew
to false, but this requires me to make a constructor for this purpose and seems a bit hacky. So I was wondering if there's basically an opposite to toObject()
推荐答案
您可以使用 Model.hydrate()
.
You can use Model.hydrate()
for this.
这篇关于根据精益查询的结果创建猫鼬模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!