gpt4 book ai didi

javascript - 在 Mongoose 中存储坐标数组,然后检索相同类型

转载 作者:行者123 更新时间:2023-12-03 11:28:09 25 4
gpt4 key购买 nike

我有这个架构

 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
]
]
}
}
]

关于javascript - 在 Mongoose 中存储坐标数组,然后检索相同类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26835128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com