如何在Mongoose模式中将数据添加到数组 [英] How to add data to array in Mongoose Schema

查看:732
本文介绍了如何在Mongoose模式中将数据添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设采用以下模式,我尝试使用Mongoose保存一些GeoJSON数据

Assuming the following schema, I am trying to save some GeoJSON data with Mongoose

var simpleSchema = new Schema({
    properties:{
        name:String,
        surname:String
    },
    location : {
        type : String,
        coordinates : [ Number , Number ]
    }
});

这就是我尝试保存文档的方式

This is how I try to save the document

var a = new simple({properties:{name:"a", surname:"b"}, location:{type:"Point", coordinates:[1, 0]}}).save(function(err){...});

但是,我在数据库中得到的是

However, what I am getting in the database is

ObjectId("542da9ab0882b41855ac3be0"), "properties" : { "name" : "a", "surname" : "b" }, "__v" : 0 }

好像整个位置标签和数据都丢失了.这是定义架构的错误方法还是保存文档的错误方法?

It looks like the whole location tag and data are missing. Is this a wrong way to define a schema or a wrong way of saving the document?

推荐答案

在嵌入式对象中使用名为type的字段时,您需要使用一个对象来定义其类型,或者Mongoose认为您正在定义的类型.对象本身.

When using a field named type in an embedded object, you need to use an object to define its type or Mongoose thinks you're defining the type of object itself.

因此将您的架构定义更改为:

So change your schema definition to:

var simpleSchema = new Schema({
    properties:{
        name:String,
        surname:String
    },
    location : {
        type : { type: String },
        coordinates : [ Number , Number ]
    }
});

这篇关于如何在Mongoose模式中将数据添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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