根据精益查询的结果创建猫鼬模型 [英] Create mongoose model from results of lean query

查看:100
本文介绍了根据精益查询的结果创建猫鼬模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用精益查询来更快地从数据库中检索大型数据集.检索到的大多数数据都是使用的,但未修改或保存,因此精益查询很好,但是我需要使用架构方法来修改和保存一小部分对象.

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屋!

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