我有这个架构
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}});
当我尝试从db
中检索文档时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。我如何确保它保持这种方式,因为我必须将这些结果传递给其他查询。
答案 0 :(得分:1)
这似乎属于无法复制的范畴。也许您可以考虑这个完整的列表示例,以查看实际代码中实际差异的位置:
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
]
]
}
}
]