Mongoose 自引用模式未为所有子文档创建 ObjectId [英] Mongoose self referenced schema not creating ObjectId for all sub documents

查看:41
本文介绍了Mongoose 自引用模式未为所有子文档创建 ObjectId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在猫鼬中有一个架构,它有一个自引用字段,如下所示:

var mongoose = require('mongoose');var CollectPointSchema = new mongoose.Schema({名称:{类型:字符串},收集点:[这个]});

当一个 CollectPoint 对象被插入时:

<代码>{"name": "级别 1"}

没关系,结果如预期:

<代码>{"_id": "58b36c83b7134680367b2547","name": "级别 1",收集点":[]}

但是当我插入自引用的子文档时,

<代码>{"name": "级别 1",收集点":[{"name": "1.1 级"}]}

它给了我这个:

<代码>{"_id": "58b36c83b7134680367b2547","name": "级别 1",收集点":[{"name": "1.1 级"}]}

CollectPointSchema_id 在哪里?我需要这个 _id.

解决方案

在声明嵌入的 CollectPoint 项时,您应该构建一个新对象:

var data = new CollectPoint({名称:1级",收集点: [新的收集点({name: "1.1 级",收集点:[]})]});

这样 _idcollectPoints 将通过 CollectPoint 的实例化创建,否则,您只是在创建一个普通的 JSONObject.

为避免此类问题,请为您的阵列构建一个 validator,这将触发如果其项目类型错误,则会出错:

var CollectPointSchema = new mongoose.Schema({名称:{ 类型:字符串},收集点:{类型:[这个],证实: {验证器:函数(v){如果 (!Array.isArray(v)) 返回 falsefor (var i = 0; i < v.length; i++) {if (!(v[i] instanceof CollectPoint)) {返回假;}}返回真;},消息:'错误的收集点格式'}}});

这样会触发一个错误:

var data = new CollectPoint({名称:1级",收集点:[{name: "1.1 级",收集点:[]}]});

I have a Schema in mongoose that have a self referenced field, like this:

var mongoose = require('mongoose');

var CollectPointSchema = new mongoose.Schema({
  name: {type: String},
  collectPoints: [ this ]
});

when a CollectPoint object is inserted:

{
  "name": "Level 1"
}

it's ok, the result is like expected:

{
  "_id": "58b36c83b7134680367b2547",
  "name": "Level 1",
  "collectPoints": []
}

But when I insert self referenced sub documents,

{
  "name": "Level 1",
  "collectPoints": [{
    "name": "Level 1.1"
  }]
}

It gives me this:

{
  "_id": "58b36c83b7134680367b2547",
  "name": "Level 1",
  "collectPoints": [{
    "name": "Level 1.1"
  }]
}

Where is the _id of the child CollectPointSchema? I need this _id.

解决方案

You should build a new object when declaring your embedded CollectPoint items :

var data = new CollectPoint({
    name: "Level 1",
    collectPoints: [
        new CollectPoint({
            name: "Level 1.1",
            collectPoints: []
        })
    ]
});

This way the _id and collectPoints will be created by the instanciation of CollectPoint otherwise, you are just creating a plain JSONObject.

To avoid those kind of issues, build a validator for your array that will trigger an error if its items have wrong type :

var CollectPointSchema = new mongoose.Schema({
    name: { type: String },
    collectPoints: {
        type: [this],
        validate: {
            validator: function(v) {
                if (!Array.isArray(v)) return false
                for (var i = 0; i < v.length; i++) {
                    if (!(v[i] instanceof CollectPoint)) {
                        return false;
                    }
                }
                return true;
            },
            message: 'bad collect point format'
        }
    }
});

This way the following will trigger an error :

var data = new CollectPoint({
    name: "Level 1",
    collectPoints: [{
        name: "Level 1.1",
        collectPoints: []
    }]
});

这篇关于Mongoose 自引用模式未为所有子文档创建 ObjectId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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