检索使用其他查询更改了字段值的文档 [英] Retrieve document with fields' value changed using another query

查看:70
本文介绍了检索使用其他查询更改了字段值的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该国的MongoDB上有该数据库,其中包含OpenStreetMap的数据,我使用 Node- Mongosm 并生成三个集合:nodeswaysrelations.

I have this database on MongoDB for my country with data of OpenStreetMap, I imported it with Node-Mongosm and produces three collections: nodes, ways and relations.

请求Way时,将输出以下文档(从mongo shell):

When requesting a Way, the following document is output (from mongo shell):

{
   "_id":492464922,
   "tags":{
      "maxspeed":"20",
      "surface":"asphalt",
      "highway":"residential",
      "oneway":"yes",
      "name":"Avenida 1"
   },
   "loc":{
      "type":"Polygon",
      "coordinates":[

      ],
      "nodes":[
         445848963,
         4844871065,
         432568566
      ]
   }
}

字段coordinates为空是出于我未知的原因(不确定这是Node-Mongosm的功能还是bug),但是我可以填充该字段,并从nodes字段中检索每个节点的坐标,即:节点445848963:

The field coordinates is empty for reasons unknown to me (not sure if this is a feature or a bug from Node-Mongosm), but I can populate it retrieving the coordinates of each node from the nodes field, i.e.: node 445848963:

{
   "_id":445848963,
   "loc":{
      "type":"Point",
      "coordinates":[
         -83.5047254,
         10.0984515
      ]
   }
}

所以,我的问题是,是否可能以及如何编写查询,该查询用于从ways集合中收集具有节点节点(ways.loc.nodes字段)动态地协调 ,因此检索到的文档看起来像这样:

So, my question is if it is possible and how should I write the query for gathering a document from ways collection with the field coordinates populated with the nodes' (ways.loc.nodes field) coordinates on the fly so the retrieved document looks like this:

{
   "_id":492464922,
   "tags":{
      "maxspeed":"20",
      "surface":"asphalt",
      "highway":"residential",
      "oneway":"yes",
      "name":"Avenida 1"
   },
   "loc":{
      "type":"Polygon",
      "coordinates":[
         -83.5047254,
         10.0984515,
         -83.5052237,
         10.0987132,
         -83.5056339,
         10.0989286
      ],
      "nodes":[
         445848963,
         4844871065,
         432568566
      ]
   }
}

推荐答案

您可以使用聚合管道

db.ways.aggregate(
[
    {$lookup : {
        from : "coords",
        localField : "loc.nodes",
        foreignField : "_id",
        as : "loc.coordinates"
    }},
    {$addFields : {
            "loc.coordinates" : {
                $reduce : {
                  input : "$loc.coordinates.loc.coordinates", 
                  initialValue :[], 
                  in : { $concatArrays : ["$$this", "$$value"] }
                }
            }
        }
    }
]
).pretty()

结果

{
    "_id" : 492464922,
    "tags" : {
        "maxspeed" : "20",
        "surface" : "asphalt",
        "highway" : "residential",
        "oneway" : "yes",
        "name" : "Avenida 1"
    },
    "loc" : {
        "type" : "Polygon",
        "coordinates" : [
            -83.5047254,
            10.0984515
        ],
        "nodes" : [
            445848963,
            4844871065,
            432568566
        ]
    }
}

这篇关于检索使用其他查询更改了字段值的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆