本文介绍了猫鼬的findOne方法检索缺少_id的有效文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到一个非常奇怪的猫鼬错误,我不知道是什么导致了这个问题.当我调用findOne方法时,我得到一个有效的实例,只有_id字段未初始化.当我尝试将更改保存在该文档中时,它会因

I'm currently on a very weird mongoose error and I don't know what is causing this problem. When I call findOne method, I get a valid instance, with only the _id field not initialized. When I'm trying to save the changes in that document it crashes with

{ [CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"]
 message: 'Cast to ObjectId failed for value "[object Object]" at path "_id"',
 name: 'CastError',
 type: 'ObjectId',
 value:
   { from_node_id: 52e10f6acce9daa7f3bf3162,
     target_id: 'a' },
 path: '_id' }

我有一个简单的架构定义:

I have a simple Schema Definition:

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var transitionProbabilitySchema;
transitionProbabilitySchema = new Schema({
   _id: {
       from_node_id: {type: Schema.Types.ObjectId, ref: "Zone"}
   },
   value: {
       total_transition_count: Number,
       probabilities: [
           {to_node_id: {type: Schema.Types.ObjectId, ref: "Zone"}},
           {probability: Number}
       ]}
}, {collection: 'transitionProbability'});

module.exports = mongoose.model('transitionProbability', transitionProbabilitySchema);

现在我打电话给

TransitionProbability.findOne({"_id.from_node_id": from_node_id},
        function (err, tp) {
            if (err) {
                handleError('error - tp not found', err, callback);
            } else {
                 // some modification of tp and then save
            }
        }

猫鼬返回文档,但只有_id未初始化.当我尝试保存更改后的文档时,而不是之前,整个功能崩溃.我还将该文档记录到控制台以验证它只是缺少"_id"字段.

Mongoose returns the document, but only _idis not initialized. The whole function crashes when I am trying to save the altered document and not before. I also logged the document to the console to verify it is just the `_id' field missing.

PS:请注意,from_node_id是从另一个查询中提取的,并且是有效的ObjectId.

PS: note, that the from_node_id is extracted from another query and is a valid ObjectId.

有什么想法吗?预先感谢您的帮助!

Any ideas? Thanks in advance for your help!

手动设置_id字段似乎也不起作用.甚至更有趣:尝试其他方法时,我意识到例如findOneAndUpdate确实冻结了整个node.js应用程序,没有任何日志.只是不会继续.

setting the _id field manually does not seem to work either. Even more interesting: while trying out other methods, I recognized that findOneAndUpdatefor example does freeze the whole node.js app, without any log. It just doesn't continue.

推荐答案

供您参考:在连续两天没有找到答案的情况下,我在Mongoose.js Github上创建了一张可能存在错误的故障单,他们确认了我的想法.问题. 根据它们,它在新的4.0.0版本候选版本中已得到修复,不建议这样做供生产使用.在实际上解决了我的问题,但是rc1提出了更多问题.

For your information: after searching for two days straight without finding an answer I created a ticket for a possible bug at Mongoose.js Github and they confirmed my problem. According to them it is fixed in the new 4.0.0 release candidate, which is not recommended for productive use. At actually solved my problem, but the rc1 made even more problems.

到目前为止,我的解决方案:

My solution so far:

最后,我对这个错误感到非常恼火,以至于我更改了该表的整个累积量,以致_id没有单独的from_node_id字段.我现在直接将from_node_id用作ID.

Finally I was so annoyed by this error, that I changed my whole cumulation of that table so that _id does not have a separate from_node_id field. I use the from_node_id now as the ID directly.

这篇关于猫鼬的findOne方法检索缺少_id的有效文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:07