如何基于数组元素向 mongoDB 集合文档添加新字段
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用聚合更新来允许像这样使用 $arrayElemAt :
示例此处
You can use an aggregation update to be allowed to use $arrayElemAt like this:
Example here