Reputation: 243
I want to save lat, long into mongodb with Node.js and mongoose. The mongodb coordinates should looks like this in the end:
{ "_id" : ObjectId("57faa6b5a005aa179276d25d"),
"loc" : { "coordinates" : [ 40.730610, -73.935242 ], "type" : "Point" }}
Node.js saving into mongodb but this is not working for me
router.post('/setData',function(req,res){
exp = test();
exp.loc = {"loc":{"type":"Point","coordinates":[40.730610, -73.935242]}};
});
exp.save(function(err,doc){
console.log("done");
});
MongoDB- after saved into MongoDB
{ "_id" : ObjectId("57faa6b5a005aa179276d25d"),"loc" : { "_id" : ObjectId("57faa6b5a005aa179276d25e"), "coordinates" : [ 0, 0 ], "type" : "Point" }}
Schema
var geoSchema = new mongoose.Schema({
type: {
type: String,
enum: 'Point',
default: 'Point'
},
coordinates: {
type: [Number],
default: [0, 0]
}
});
How can I save the data that the mongodb property looks like so. The id is generated automatically:
{ "_id" : ObjectId("57faa6b5a005aa179276d25d"),
"loc" : { "coordinates" : [ 40.730610, -73.935242 ], "type" : "Point" }}
Upvotes: 0
Views: 3780
Reputation: 11930
This is incorrect
exp.loc = {"loc":{"type":"Point","coordinates":[40.730610, -73.935242]}};
Must be
exp.loc = {"type":"Point","coordinates":[40.730610, -73.935242]};
Upvotes: 1
Reputation: 514
Missing loc
in your schema:
var geoSchema = new mongoose.Schema({
loc: {
type: Object,
properties: {
type: {
type: String,
enum: 'Point',
default: 'Point'
},
coordinates: {
type: [Number],
default: [0, 0]
}
}
});
Upvotes: 3