【问题标题】:How to use ensureIndex with geoJSON on node.js?如何在 node.js 上将 ensureIndex 与 geoJSON 一起使用?
【发布时间】: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


    【解决方案1】:

    修好了!您必须在数据库(而不是集合)上调用 ensureIndex,并将集合作为第一个参数传递,然后是索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多