我在 PHP 中插入带有地理空间数据的 Mongodb 记录时遇到问题

发布于 2025-01-07 03:18:33 字数 467 浏览 2 评论 0原文

我正在尝试插入包含地理空间数据嵌套数组的记录。我什至尝试将数组变成一个对象,但也没有运气。或多或少是插入行:

$collection->insert($obj, array('safe'=>true));

一件有趣的事情是,当我将索引“loc”称为其他名称(例如整数)时,插入会起作用。

这是应该进入 Mongodb 但没有进入的数组:

Array
(
  [name] => Home
  [address] => 500 Pole Line Road
  [city] => Davis
  [state] => CA
  [zip] => 95618
  [loc] => Array
    (
      [lon] => -121.726710
      [lat] => 38.549576
    )

)

I'm trying to insert a record that contains a nested array of geospatial data. I've even tried making the array an object instead, but no luck there either. Here's more or less the insert line:

$collection->insert($obj, array('safe'=>true));

One interesting thing is that when I call the index 'loc' something else, such as just an integer, the insert works.

Here's the array that should be going into Mongodb, but isn't:

Array
(
  [name] => Home
  [address] => 500 Pole Line Road
  [city] => Davis
  [state] => CA
  [zip] => 95618
  [loc] => Array
    (
      [lon] => -121.726710
      [lat] => 38.549576
    )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾`听者〃 2025-01-14 03:18:33

MongoDB 不关心地理空间“字段”的键。它只需要前两个元素,第一个元素是经度,第二个元素是纬度。即使您将第一个名称命名为“lat”,它仍然会被视为经度。我认为它不会插入的唯一原因可能是您在某些字段上有一个唯一的键。

如果您添加要插入的内容的完整工作(但很小)示例,那么我可以更新答案以希望显示出问题所在。

MongoDB doesn't care about the keys for a geospational "field". It just takes the first two elements with the first one being longitude, and the second lattitude. Even if you'd name your first one "lat" it would still be seen as the longitude. The only reason why I could think it would not insert is perhaps you have a unique key on some fields.

If you'd add a full working (but small) example of what you're inserting, then I can update the answer to hopefully show what is wrong.

青春如此纠结 2025-01-14 03:18:33
array( 'loc' => array( floatval($lon),  floatval($lat)))
array( 'loc' => array( floatval($lon),  floatval($lat)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文