mongodb-将字段名称插入字符串而不是获取值

发布于 2025-01-25 01:26:41 字数 539 浏览 0 评论 0原文

我想从已经存在的经度和纬度创建一个新的位置字段。

db.neigborhood.updateMany({}, {
$set: {
    "location": {
        "type": "Point",
        "coordinates": ["$longitude", "$latitude"]
    }   
}});

我编写了应该创建新字段的代码,但是问题是我将名称作为字符串而不是字段值。

{
  "_id": {
    "$oid": "626a01f1df85b4b2937ece2d"
  },
  "latitude": "10.4980067",
  "longitude": "-66.8335096",
  "location": {
    "type": "Point",
    "coordinates": [
      "$longitude",
      "$latitude"
    ]
  }
}

我做错了什么,因为我得到了“ $经度”而不是-66.8335096值?

I want to create a new location field from already existing longitude and latitude.

db.neigborhood.updateMany({}, {
$set: {
    "location": {
        "type": "Point",
        "coordinates": ["$longitude", "$latitude"]
    }   
}});

I wrote this code that should create the new field, but the problem is that instead of the field values I get the names as strings.

{
  "_id": {
    "$oid": "626a01f1df85b4b2937ece2d"
  },
  "latitude": "10.4980067",
  "longitude": "-66.8335096",
  "location": {
    "type": "Point",
    "coordinates": [
      "$longitude",
      "$latitude"
    ]
  }
}

What am I doing wrong that I get "$longitude" instead of the -66.8335096 value?

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

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

发布评论

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

评论(1

江心雾 2025-02-01 01:26:41

带有聚合管道的更新。

db.neigborhood.updateMany({},
[
  {
    $set: {
      "location": {
        "type": "Point",
        "coordinates": [
          "$longitude",
          "$latitude"
        ]
      }
    }
  }
])

示例mongo playground

Works with Update with Aggregation Pipeline.

db.neigborhood.updateMany({},
[
  {
    $set: {
      "location": {
        "type": "Point",
        "coordinates": [
          "$longitude",
          "$latitude"
        ]
      }
    }
  }
])

Sample Mongo Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文