为什么mongo中的新文档具有对象而不是ObjectId? [英] Why new documents in mongo have an object and not an ObjectId?

查看:48
本文介绍了为什么mongo中的新文档具有对象而不是ObjectId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mongodb中插入新文档时,id看起来不像ObjectId,而是看起来像一个对象.

When inserting new documents in mongodb, ids don't look like ObjectId and instead they look like an object.

"_id" : {
        "_bsontype" : "ObjectID",
        "id" : "U\u0013[-Ф~\u001d$©t",
        "generationTime" : 1.43439e+09
}

预期类型:

"_id" : ObjectId("55107edd8e21f20000fd79a6")

我的mongodb版本是3.0.3,这几乎是代码和架构

My mongodb version is 3.0.3 and this is pretty much the code and the schema

var Script = {
    run: function() {
        return CourseModel.findQ()
        .then(function(courses){
            return courses.map(worker);
        }).catch(function(error){
            console.log(error);
        });
    }
};

function worker(course){
    var category = { name: course.name, displayOrder: 0 };
        return CategoryModel.createQ(category).then(function() {
            course.set('name', undefined);
            return course.saveQ();
        });
}
module.exports = Script;

var CategorySchema = new Schema({
    name: {
        type: String,
        required: true,
        unique: true
    },
    active: {
        type: Boolean,
        default: true
    },
    displayOrder: Number,
    updateDate: Date,
    subcategories: [{
        type: Schema.Types.ObjectId,
        ref: 'subcategories'
    }]
});

推荐答案

畸形的ObjectId是由与mongoose-q使用的mongoose版本冲突引起的.您需要将mongoose-q更新到版本0.1.0.我以前使用的是0.0.17,并且看到的行为与您在此处看到的完全相同.

The malformed ObjectIds are caused by a conflict with the mongoose version that mongoose-q is using. You'll need to update to mongoose-q to version 0.1.0. I was using 0.0.17 previously and saw exactly the same behavior that you saw here.

这篇关于为什么mongo中的新文档具有对象而不是ObjectId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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