如何基于数组元素向 mongoDB 集合文档添加新字段

发布于 2025-01-14 17:05:01 字数 2193 浏览 2 评论 0原文

我的 mongoDB 集合中有以下形状的文档:

{
        "_id" : ObjectId("622afbb16c5bba83829e8033"),
        "type" : "Feature",
        "properties" : {
                "mag" : 0.6,
                "place" : "6km NW of The Geysers, California",
                "time" : NumberLong("1370255770900"),
                "updated" : NumberLong("1370256367681"),
                "tz" : -420,
                "url" : "http://earthquake.usgs.gov/earthquakes/eventpage/nc72001595",
                "detail" : "http://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72001595.geojson",
                "cdi" : null,
                "mmi" : null,
                "alert" : null,
                "status" : "AUTOMATIC",
                "tsunami" : null,
                "sig" : 6,
                "net" : "nc",
                "code" : "72001595",
                "ids" : ",nc72001595,",
                "sources" : ",nc,",
                "types" : ",general-link,geoserve,nearby-cities,origin,phase-data,scitech-link,",
                "nst" : null,
                "dmin" : 0.01796631,
                "rms" : 0.05,
                "gap" : 205.2,
                "magType" : "Md",
                "type" : "earthquake",
                "iso_date" : ISODate("2013-06-03T10:36:10.900Z"),
                "types_as_array" : [
                        "general-link",
                        "geoserve",
                        "nearby-cities",
                        "origin",
                        "phase-data",
                        "scitech-link"
                ]
        },
        "geometry" : {
                "type" : "Point",
                "coordinates" : [
                        -122.8173,
                        38.8115,
                        9.4
                ]
        },
        "id" : "nc72001595"
}

我想添加一个新的字段,称为“深度”,基于 Geometry.coordinates 数组的第三个元素。

这是我到目前为止所得到的:

db.earthquakes.updateMany({}, {$set:{"depth":"$geometry.coordinates.2"}} )

但它只给我字符串而不是第三个值。

"id" : "nc72001620",
"depth" : "$geometry.coordinates.2"

但我想得到的是

"id" : "nc72001620",
"depth" : "9.4"

I have documents of the following shape in my mongoDB collection:

{
        "_id" : ObjectId("622afbb16c5bba83829e8033"),
        "type" : "Feature",
        "properties" : {
                "mag" : 0.6,
                "place" : "6km NW of The Geysers, California",
                "time" : NumberLong("1370255770900"),
                "updated" : NumberLong("1370256367681"),
                "tz" : -420,
                "url" : "http://earthquake.usgs.gov/earthquakes/eventpage/nc72001595",
                "detail" : "http://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72001595.geojson",
                "cdi" : null,
                "mmi" : null,
                "alert" : null,
                "status" : "AUTOMATIC",
                "tsunami" : null,
                "sig" : 6,
                "net" : "nc",
                "code" : "72001595",
                "ids" : ",nc72001595,",
                "sources" : ",nc,",
                "types" : ",general-link,geoserve,nearby-cities,origin,phase-data,scitech-link,",
                "nst" : null,
                "dmin" : 0.01796631,
                "rms" : 0.05,
                "gap" : 205.2,
                "magType" : "Md",
                "type" : "earthquake",
                "iso_date" : ISODate("2013-06-03T10:36:10.900Z"),
                "types_as_array" : [
                        "general-link",
                        "geoserve",
                        "nearby-cities",
                        "origin",
                        "phase-data",
                        "scitech-link"
                ]
        },
        "geometry" : {
                "type" : "Point",
                "coordinates" : [
                        -122.8173,
                        38.8115,
                        9.4
                ]
        },
        "id" : "nc72001595"
}

And I would like to add a new field call "depth" based on the 3rd element of the geometry.coordinates array.

Here is what I have so far:

db.earthquakes.updateMany({}, {$set:{"depth":"$geometry.coordinates.2"}} )

But it only gives me the string and not the 3rd value.

"id" : "nc72001620",
"depth" : "$geometry.coordinates.2"

But what I would like to get is

"id" : "nc72001620",
"depth" : "9.4"

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

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

发布评论

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

评论(1

拥有 2025-01-21 17:05:01

您可以使用聚合更新来允许像这样使用 $arrayElemAt

db.collection.update({},
[
  {
    $set: {
      "depth": {
        "$arrayElemAt": [
          "$geometry.coordinates",
          2
        ]
      }
    }
  }
])

示例此处

You can use an aggregation update to be allowed to use $arrayElemAt like this:

db.collection.update({},
[
  {
    $set: {
      "depth": {
        "$arrayElemAt": [
          "$geometry.coordinates",
          2
        ]
      }
    }
  }
])

Example here

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