本文介绍了将普通对象转换为mongoose文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新1:已收到5票,因此我提交了一项功能请求:

UPDATE 1: 5 votes have been received, so I have submitted a feature request: https://github.com/LearnBoost/mongoose/issues/2637

请将你的+1票投下去让核心团队知道你想要这个功能。

Please cast your +1 votes there to let the core team know you want this feature.

更新2: ...

原始帖子:

让我说我对一个集合进行精益查询 OR 从REST服务接收一些数据,我得到一个对象数组(而不是mongoose文档)。

Lets say I do a "lean" query on a collection OR receive some data from a REST service and I get an array of objects (not mongoose documents).

这些对象已存在于数据库中,但是我需要将部分/全部对象转换为mongoose文档以进行单独编辑/保存。

These objects already exist in the database, but I need to convert some/all of those objects to mongoose documents for individual editing/saving.

我已经阅读了源代码一旦mongoose拥有来自数据库的数据(填充,转换,初始化等),就会发生很多事情,但似乎没有一种方法可以将这种方法暴露给外界。

I have read through the source and there is a lot going on once mongoose has data from the database (populating, casting, initializing, etc), but there doesn't seem to be a method for 'exposing' this to the outside world.

我使用以下内容,但它看起来很hacky( $ data 是一个普通对象):

I am using the following, but it just seems hacky ($data is a plain object):

// What other properties am I not setting?  Is this enough?
var doc = new MyModel( $data );
doc.isNew = false;

// mimicking mongoose internals
// "init" is called internally after a document is loaded from the database
// This method is not documented, but seems like the most "proper" way to do this.
var doc = new MyModel( undefined );
doc.init( $data );

更新:经过更多搜索后,我认为没有办法要做到这一点,上面的第一种方法是你最好的选择(mongoose v3.8.8)。如果其他人对此感兴趣,我会针对此类内容发出功能请求(请发表评论或发表评论):

UPDATE: After more searching I don't think there is a way to do this yet, and the first method above is your best bet (mongoose v3.8.8). If anybody else is interested in this, I will make a feature request for something like this (leave a comment or upvote please):

var doc = MyModel.hydrate( $data );


推荐答案

发布我自己的答案,这样就不会保持开放状态

版本4模型(2015-03-25发布的稳定版)现在公开了方法。这些字段最初都不会被标记为 dirty ,这意味着对 save()的调用在字段发生变异之前不会执行任何操作。

Version 4 models (stable released on 2015-03-25) now exposes a hydrate() method. None of the fields will be marked as dirty initially, meaning a call to save() will do nothing until a field is mutated.

请注意这一点非常重要这旨在用于将从数据库加载的普通JS对象转换为mongoose文档。如果您从REST服务或类似的服务接收文档,则应使用 findById() update()

It is very important to note that this is intended to be used to convert a plain JS object loaded from the database into a mongoose document. If you are receiving a document from a REST service or something like that, you should use findById() and update().

对于那些生活危险的人:

如果你真的想更新一个现有文档没有触及数据库,我想你可以调用 hydrate(),将字段标记为脏,然后调用 save()。这与我在原始问题中建议的设置 doc.isNew = false; 的方法没有什么不同(除了你不必将字段标记为脏)。然而,瓦列里(来自猫鼬队)建议不要这样做。它可能会在某些情况下导致验证错误(和其他问题),通常不是好的做法。

If you really want to update an existing document without touching the database, I suppose you could call hydrate(), mark fields as dirty, and then call save(). This is not too different than the method of setting doc.isNew = false; as I suggested in my original question (except you don't have to mark fields as dirty). However, Valeri (from the mongoose team) suggested not doing this. It could cause validation errors (and other issues) under certain scenarios and generally isn't good practice.

这篇关于将普通对象转换为mongoose文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:05
查看更多