Mongoose findOne 方法检索缺少 _id 的有效文档
Mongoose findOne method retrieves valid document with missing _id
我目前遇到一个非常奇怪的猫鼬错误,我不知道是什么导致了这个问题。当我调用 findOne
方法时,我得到一个有效实例,只有 _id
字段未初始化。当我尝试保存该文档中的更改时,它崩溃并显示
{ [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' }
我有一个简单的架构定义:
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
}
}
Mongoose returns文件,但只有_id
没有初始化。整个功能在我尝试保存更改后的文档时崩溃,而不是之前。我还将文档记录到控制台以验证它只是缺少“_id”字段。
PS:请注意,from_node_id
是从另一个查询中提取的,并且是有效的 ObjectId
。
有什么想法吗?预先感谢您的帮助!
编辑:手动设置 _id 字段似乎也不起作用。更有趣的是:在尝试其他方法时,我认识到 findOneAndUpdate
例如确实冻结了整个 node.js 应用程序,没有任何日志。它只是没有继续。
编辑:供您参考:在连续搜索两天没有找到答案后,我在 Mongoose.js Github 为可能的错误创建了一张票,他们确认了我的问题。 According to them it is fixed in the new 4.0.0 release candidate,不推荐用于生产用途。其实at解决了我的问题,但是rc1的问题更多。
目前我的解决方案:
最后,我对这个错误感到非常恼火,以至于我更改了 table 的整个累积,以便 _id
没有单独的 from_node_id
字段。我现在直接用from_node_id
作为ID。
我目前遇到一个非常奇怪的猫鼬错误,我不知道是什么导致了这个问题。当我调用 findOne
方法时,我得到一个有效实例,只有 _id
字段未初始化。当我尝试保存该文档中的更改时,它崩溃并显示
{ [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' }
我有一个简单的架构定义:
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
}
}
Mongoose returns文件,但只有_id
没有初始化。整个功能在我尝试保存更改后的文档时崩溃,而不是之前。我还将文档记录到控制台以验证它只是缺少“_id”字段。
PS:请注意,from_node_id
是从另一个查询中提取的,并且是有效的 ObjectId
。
有什么想法吗?预先感谢您的帮助!
编辑:手动设置 _id 字段似乎也不起作用。更有趣的是:在尝试其他方法时,我认识到 findOneAndUpdate
例如确实冻结了整个 node.js 应用程序,没有任何日志。它只是没有继续。
编辑:供您参考:在连续搜索两天没有找到答案后,我在 Mongoose.js Github 为可能的错误创建了一张票,他们确认了我的问题。 According to them it is fixed in the new 4.0.0 release candidate,不推荐用于生产用途。其实at解决了我的问题,但是rc1的问题更多。
目前我的解决方案:
最后,我对这个错误感到非常恼火,以至于我更改了 table 的整个累积,以便 _id
没有单独的 from_node_id
字段。我现在直接用from_node_id
作为ID。