How create a new array field with the aggregate framework

前端 未结 4 2045
礼貌的吻别
礼貌的吻别 2021-02-15 15:51

I\'m starting to use mongoDb and I\'m stuck with a simple use case.

Let\'s say I\'ve got a collection \'aCollection\' with entries such as this:



        
4条回答
  •  既然无缘
    2021-02-15 16:21

    In Modern MongoDB releases the most efficient way is to simply notate the array using the existing document properties. Direct notation of arrays was introduced in MongoDB 3.2:

    db.collection.aggregate([
      { "$project": {
        "lat": 1,
        "long": 1,
        "geometry": {
          "type": { "$literal": "Point" },
          "coordinates": [ "$lat", "$long" ]
        }
      }},
      { "$out": "newcollection" }
    ])
    

    Or even using $addFields to simply "append" the new property to the documents:

    db.collection.aggregate([
      { "$addFields": {
        "geometry": {
          "type": { "$literal": "Point" },
          "coordinates": [ "$lat", "$long" ]
        }
      }},
      { "$out": "newcollection" }
    ])
    

    If you are using MongoDB 2.6 and above you can do this with the aggregation framework and avoid looping results in your client program in order to create a new collection.

    The main feature here that help you are the $out operator for sending the output to a new collection. But also being a little clever in order to create the array that you need.

    db.collection.aggregate([
        { "$project": {
            "lat": 1,
            "long": 1,
            "type": { "$literal": ["lat","long"] }
        }},
        { "$unwind": "$type" },
        { "$group": {
            "_id": "$_id",
            "lat": { "$first": "$lat" },
            "long": { "$first": "$long" },
            "coordinates": {
                "$push": {
                    "$cond": [
                        { "$eq": [ "$type", "lat" ] },
                        "$lat",
                        "$long"
                    ]
                }
            }
        }},
        { "$project": {
            "lat": 1,
            "long": 1,
            "geometry": { 
                "type": { "$literal": "Point" },
                "coordinates": "$coordinates"
            }
        }},
        { "$out": "newcollection" }
    ])
    

    So this makes use of the $literal operator in order to specify a new array at the head of the pipeline. This operator will put content in the document property exactly how it is supplied. So no variable substitutions are allowed, hence "literal".

    In order to create the "coordintes" array, we simply unwind that first array which essentially creates two of every document with a different value in "type". This is then used in the $group stage to conditionally $push either the "$lat" or "$long" value onto that array.

    Finally use $project again to finalize the document structure and then $out sends all output to the new collection.


    Note that this only makes sense if your intention is to create a new collection and avoid sending traffic "over the wire". This could not be used purely within the aggregation framework to re-shape your document with the intent to then do a "geo-spatial" query in that same aggregation pipeline as "geo-spatial" queries will only work when actually indexed on a collection.

    So this may help you create a new collection as you want to, but at least it serves as example ( or two examples actually ) of how to create an array's out of different values with the aggregation framework.

提交回复
热议问题