【问题标题】:location object expected, location array not in correct format, mongo 3.6 + Phalcon Php预期位置对象,位置数组格式不正确,mongo 3.6 + Phalcon Php
【发布时间】:2018-07-09 00:16:46
【问题描述】:

我正在开发一个处理地理位置信息的 MongoDB 集合。所以,

  1. 我创建了一个下面的集合索引。

    db.places.createIndex( { location: "2dsphere" } )
    
  2. 我通过PHP插入并找到了一条记录(使用phalcon)如下

    $lng = (float) -73.9667; 
    $lat = (float) 40.78;
    $m = new Store();
    $m->location = [
       "type" => 'Point', 
       "coordinates" => [$lng, $lat]
    ] ;
    print_r($m->location);
    $m->save();
    ... 
    
    $params = [
       [ 
           "location" => [ 
               '$near' => [ 
                    '$geometry' => [ 
                         "type" => "Point", 
                          "coordinates" => [ $lng, $lat ]
                    ],
                    '$minDistance' => 1,
                    '$maxDistance' => 5000
                ]
           ]
       ]
    ];
    
    
    $find = Store::find($params);
    print_r(count($find));
    

我收到以下错误消息:

PHP Fatal error:  Uncaught exception 'MongoWriteConcernException' 
with message 'localhost:27017: location object expected, location array 
not in correct format' in /var/www/html/muyal/cli/tasks/MedicineTask.php:51

我认为 2dshere 格式不正确,是吗?

【问题讨论】:

    标签: php mongodb phalcon


    【解决方案1】:

    错误信息说你需要传递位置对象,但是你传递了位置数组。

    关于 2dsphere 的 MongoDB 文档使用 JSON 对象

    https://docs.mongodb.com/manual/core/2dsphere/

    db.places.insert(
       {
           loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
           name: "Central Park",
           category : "Parks"
       }
    )
    

    所以我想解决方案是将数组类型转换为对象,例如:

    $m->location = (object) [
        "type" => 'Point', 
        "coordinates" => [$lng, $lat]
    ];
    

    【讨论】:

      猜你喜欢
      • 2013-12-04
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多