[英]storing a array of coordinates in mongoose and then retrieving the same type
我有这个架构
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;
但是,每当我使用此架构存储JSON时
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}});
当我尝试从数据库中检索文档时
newFence1.find({}).lean().exec(function(err,docs){
console.log('docsss '+ JSON.stringify(docs));
console.log ( 'coordinates'+docs[0].loc.coordinates);
}
docs [0] .loc.coordinates的形式与坐标数组不同,而是仅由逗号分隔的所有数字,例如从[[12,12],[12,3]]到===> 12,12,12,13。 我如何确保它保持这种状态,因为我必须将那些结果传递给其他查询。
这似乎属于无法复制的范畴。 也许您可以考虑下面的完整清单示例,以查看实际代码中的实际差异所在:
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();
});
});
可能值得一提的是,以下表示法与“混合”类型完全相同,只是隐式表示“缺少”和已定义类型:
var fenceSchema = new Schema({
"loc": {
"type": { "type": String },
"coordinates": []
},
"created": { "type": Date, "default": Date.now }
});
基本上,这提供了以下输出:
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
]
]
}
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.