【发布时间】:2014-04-04 19:51:27
【问题描述】:
我对 node.js 和 mongodb 非常陌生,因此我们将不胜感激。我曾尝试在 collection.update 操作之前和之后使用 ensureIndex,但无论哪种方式,它似乎都开始出错。
有人能指出我正确的方向吗?
app.post("/", function(req, res){
var jsonObj = req.body;
var username = jsonObj['Username'];
var longitude = jsonObj['longitude'];
var latitude = jsonObj['latitude'];
var geoJsonObj = {'Username': username, location: {"type" : "Point", "coordinates" : [longitude, latitude]}};
//res.send(req.body);
mongo.Db.connect(mongoUri, function (err, db) {
if (err)
res.send(null);
db.collection('catchmerequests', function(err, collection) {
if (err)
res.send(null);
db.collection.ensureIndex( { location : "2dsphere" }, function (err, collection) {
collection.update({'Username':username}, {$set: geoJsonObj}, {upsert:true}, function(err,result) {
if (err)
res.send(null);
else
res.send(jsonObj);
});
});
});
});
});
在没有 ensureIndex 的情况下运行以下代码可以完美运行!
【问题讨论】:
标签: node.js mongodb heroku indexing geojson