user1493786
user1493786

Reputation: 325

storing a array of coordinates in mongoose and then retrieving the same type

i have this schema

 var mongoose = require('mongoose');

var fenceSchema = mongoose.Schema({
FenceID : {type: String},
loc :{
    type: {
        type: String,
    },
    coordinates: [mongoose.Schema.Types.Mixed]
}
,
created : {type: Date, default: Date.now}
});

var fences = mongoose.model('fence1',fenceSchema);
module.exports = fences;

However whenever I store a JSON using this schema

var pointB = [[43.647228, -79.404012],[43.647869, -79.377363],[43.622821, -79.375429],[43.622082, -79.40385     7]];     
var post =  newFence1({ FenceID: "XSDF", loc :{type: 'Polygon', coordinates: pointB}});

And when I try to retrieve the document from the db

newFence1.find({}).lean().exec(function(err,docs){
     console.log('docsss '+ JSON.stringify(docs));
     console.log ( 'coordinates'+docs[0].loc.coordinates);
}

The docs[0].loc.coordinates doesn't stay the same form as an array of coordinates instead its just all the numbers delimitated by comma e.g from [[12,12],[12,3]] to ===> 12,12,12,13. How do I ensure it stays that way because I have to pass those results for some other query.

Upvotes: 0

Views: 1223

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151112

This would seem to come under the category of cannot reproduce. Perhaps you can consider this full listing example to see where the actual differences are in your actual code:

mongoose.connect('mongodb://localhost/test');

var fenceSchema = new Schema({
  "loc": {
    "type": { "type": String },
    "coordinates": [Schema.Types.Mixed]
  },
  "created": { "type": Date, "default": Date.now }
});

var Fence = mongoose.model( 'Fence', fenceSchema );

var myPoly = [
  [43.647228, -79.404012],
  [43.647869, -79.377363],
  [43.622821, -79.375429],
  [43.622082, -79.403857]
];

var post = new Fence({
  "loc": {
    "type": "Polygon",
    "coordinates": myPoly
  }
});

console.log(
  "Before Save:\n%s", JSON.stringify( post, undefined, 4 ) );

post.save(function(err,doc) {
  if (err) throw err;

  console.log(
    "After Save:\n%s", JSON.stringify( doc, undefined, 4 ) );

  Fence.find({ "_id": doc._id },function(err,docs) {
    if (err) throw err;
    console.log(
      "When Found:\n%s",JSON.stringify( docs, undefined, 4 ) );
    process.exit();
  });

});

Probably worth mentioning that the following notation is exactly the same as the "Mixed" type, by implicit "lack of" and defined type:

var fenceSchema = new Schema({
  "loc": {
    "type": { "type": String },
    "coordinates": []
  },
  "created": { "type": Date, "default": Date.now }
});

This gives the following output, basically:

Before Save:
{
    "_id": "54605dd572dab34c6405a042",
    "created": "2014-11-10T06:40:21.020Z",
    "loc": {
        "type": "Polygon",
        "coordinates": [
            [
                43.647228,
                -79.404012
            ],
            [
                43.647869,
                -79.377363
            ],
            [
                43.622821,
                -79.375429
            ],
            [
                43.622082,
                -79.403857
            ]
        ]
    }
}
After Save:
{
    "__v": 0,
    "_id": "54605dd572dab34c6405a042",
    "created": "2014-11-10T06:40:21.020Z",
    "loc": {
        "type": "Polygon",
        "coordinates": [
            [
                43.647228,
                -79.404012
            ],
            [
                43.647869,
                -79.377363
            ],
            [
                43.622821,
                -79.375429
            ],
            [
                43.622082,
                -79.403857
            ]
        ]
    }
}
When Found:
[
    {
        "_id": "54605dd572dab34c6405a042",
        "__v": 0,
        "created": "2014-11-10T06:40:21.020Z",
        "loc": {
            "type": "Polygon",
            "coordinates": [
                [
                    43.647228,
                    -79.404012
                ],
                [
                    43.647869,
                    -79.377363
                ],
                [
                    43.622821,
                    -79.375429
                ],
                [
                    43.622082,
                    -79.403857
                ]
            ]
        }
    }
]

Upvotes: 1

Related Questions