MongoDB中,我有两个集合使用,这将帮助我解答"Where are the cell towers in my state?"
我的node.js MongoDB 2dsphere find()调用有什么问题?
首先收集从人口普查数据建立的问题,它有国家边界的多边形。
二集是从下载OpenCellId数据库和进口它创造。 “l”是塔的位置坐标键。
的JavaScript代码需要获得的状态,然后在该状态下打印塔。
我已经使用内华达州多边形的文本(复制/粘贴)代替find()
调用中的stateBounds变量来完成此操作。
但通过完全相同的对象,而不是复制/粘贴时,该console.log("Tower found: %j", item);
调用打印"Tower found: [Circular]"
。
const mongoUrl = 'mongodb://localhost:27017/test';
const hostname = '127.0.0.1';
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(mongoUrl, function(err, db){
var towers = db.collection('celltowers');
var states = db.collection('states');
states.findOne({"properties.STUSPS": "NV"},function(err, item){
getTowers(towers, item);
});
});
function getTowers(celltowers, state) {
var stateBounds = JSON.stringify(state, "geometry.coordinates");
celltowers.find({l:{$geoWithin:{$geometry:{type: "Polygon", coordinates: stateBounds}}}}, function(err, item) {
console.log("Tower found: %j", item);
});
}
我需要为celltowers.find()
使用geojson多边形。
我能做些什么来解决这个问题?
你能告诉什么是这行代码中的'JSON.stringify(州,“geometry.coordinates”)的意图;'?你可以添加控制台并检查'stateBounds'的值吗?这是否合适? – Shrabanee
JSON.stringify选择geojson状态的属性。我只需要坐标和地址(如果您愿意)选择geometry.coordinates。通过复制并粘贴该调用的结果并替换stateBounds,find()起作用。所以我认为这是正确的,因为复制/粘贴工作。 –
你试过用Json的方式。丝袜不是正确的方法。请参阅一些文档,以了解如何从Json对象获取所需的字段。 – Shrabanee