skip to Main Content

I would like to create a query to calculate the average geographical position of all tree species in MongoDb.My code is here:

from pymongo import MongoClient
from utils import get_my_password, get_my_username
from pprint import pprint


client = MongoClient(
    host='127.0.0.1',
    port=27017,
    username=get_my_username(),
    password=get_my_password(),
    authSource='admin'
)

db = client['paris']
col = db['trees']

pprint(col.find_one())

{'_id': ObjectId('5f3276d8c22f704983b3f681'),
 'adresse': 'JARDIN DU CHAMP DE MARS / C04',
 'arrondissement': 'PARIS 7E ARRDT',
 'circonferenceencm': 115.0,
 'domanialite': 'Jardin',
 'espece': 'hippocastanum',
 'genre': 'Aesculus',
 'geo_point_2d': [48.8561906007, 2.29586827747],
 'hauteurenm': 11.0,
 'idbase': 107224.0,
 'idemplacement': 'P0040937',
 'libellefrancais': 'Marronnier',
 'remarquable': '0',
 'stadedeveloppement': 'A',
 'typeemplacement': 'Arbre'}

I tryed to do it with next lines:

cursor = col.aggregate([
    {'$match': {first: $arrayElemAt: [ "$geo_point_2d", 0 ]},
               {last: $arrayElemAt: [ "$geo_point_2d", 1 ]}},
    {'$group': {'_id': '$espece', 'GeoMeanTreeSpieces': {'$avg': 1}}},
    {'$project': {'_id': 0, 'espece': '$_id', 'GeoMeanTreeSpieces': '$GeoMeanTreeSpieces'}}
])

print(list(cursor))

It doesn’t work. I need more and more practices. I got this error:

File "<ipython-input-9-2a108c75c8c4>", line 20
    {'$match': {first: $arrayElemAt: [ "$geo_point_2d", 0 ]},
                   ^
SyntaxError: invalid syntax

Could you help me please to find a solution please?
Best regards

2

Answers


  1. Chosen as BEST ANSWER

    I found out the SOLUTION of this task:

    pipeline=[
          {"$group": {"_id": "$espece", 
                      "x_moyen": {"$avg": {"$arrayElemAt":["$geo_point_2d",0]}},
                      "y_moyen": {"$avg": {"$arrayElemAt":["$geo_point_2d",1]}},
                     }},                   
    ]
    
    pprint(list(col.aggregate(pipeline=pipeline))[:10])
    

    And the answer:

    [{'_id': 'rufa', 'x_moyen': 48.8370548843, 'y_moyen': 2.44577495275},
     {'_id': 'antarctica',
      'x_moyen': 48.887011929275,
      'y_moyen': 2.3707275018700003},
     {'_id': 'vulgaris',
      x_moyen': 48.82593008238334,
      'y_moyen': 2.4511533611338887},
     {'_id': 'venustula', 'x_moyen': 48.820849268, 'y_moyen': 2.45398620992},
     {'_id': 'monophylla',
      'x_moyen': 48.821262685099995,
      'y_moyen': 2.455444424045},
     {'_id': 'verticillata',
      'x_moyen': 48.82065495999999,
      'y_moyen': 2.45680436618},
     {'_id': 'x media', 'x_moyen': 48.8672036959, 'y_moyen': 2.3482812026358535},
     {'_id': 'orientalis',
      'x_moyen': 48.83334073763992,
      'y_moyen': 2.362370810808307},
     {'_id': 'typhina',
      'x_moyen': 48.872847743710864,
      'y_moyen': 2.398939027817174},
     {'_id': 'incisa', 'x_moyen': 48.85942981336666, 'y_moyen': 2.2922408362633333}]
    

    I was not clear when I ask about it. Thank you for help. Best regards


  2. If I understand correctly (this is not the actual GeoMeanTreeSpieces, the name is confusing):

    db.collection.aggregate([
      {
        $set: {
          GeoMeanTreeSpieces: {
            $divide: [
              {$add: [{$first: "$geo_point_2d"}, {$last: "$geo_point_2d"}]},
              2
            ]
          }
        }
      }
    ])
    

    See how it works on the playground example

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search