Friday 15 January 2010

node.js - storing a array of coordinates in mongoose and then retrieving the same type -



node.js - storing a array of coordinates in mongoose and then retrieving the same type -

i have 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 store json using 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 seek retrieve document 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 remain same form array of coordinates instead numbers delimitated comma e.g [[12,12],[12,3]] ===> 12,12,12,13. how ensure stays way because have pass results other query.

this seem come under category of cannot reproduce. perhaps can consider total listing illustration see actual differences in 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 next notation same "mixed" type, implicit "lack of" , defined type:

var fenceschema = new schema({ "loc": { "type": { "type": string }, "coordinates": [] }, "created": { "type": date, "default": date.now } });

this gives next 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 ] ] } } ]

node.js mongodb mongoose geojson

No comments:

Post a Comment