Dimitris
Dimitris

Reputation: 93

How to add data to array in Mongoose Schema

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?

Upvotes: 0

Views: 1314

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

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 ]
    }
});

Upvotes: 2

Related Questions