简体   繁体   English

如何在Mongoose模式中向数组添加数据

[英]How to add data to array in Mongoose Schema

Assuming the following schema, I am trying to save some GeoJSON data with Mongoose 假设采用以下架构,我尝试使用Mongoose保存一些GeoJSON数据

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? 这是定义架构的错误方法还是保存文档的错误方法?

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

So change your schema definition to: 因此,将您的架构定义更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM