How to integrate and make queries with pymongo?

Asked

Viewed 604 times

1

I am learning to integrate Python and Mongodb and, because of that, I proposed a challenge: create a small program to register football players.

My program has two modules, info_player and info_team. My intention is to run the program interactively (python -i). The first module receives information about the players and the second, information about the team, in addition to talking to the bank.

I structured the bank as follows, database is called championship, Collections are the teams and Documents are the players. That is, there is a one-to-Many relationship between teams and players.

My doubts are: I need to convert the generated objects to Json (I thought to create a method to_json)? How to save in the bank and make the desired queries?

info_player:

class Player:
    def __init__(self, name, age, nationality="brazilian", team):
        """
        initializating Jogador class
        """
        self.personal(name, age, country)
        self.professional(team)

    def personal(self, name, age, nationality, dominancy, height):
        """
        personal data about players
        """
        self.name = name
        self.age = age
        self.nationality = nationality
        self.height = height
        self.dominancy = dominancy  # righty, lefty or ambidextrous

    def profissional(self, position, number, team, primary):
        """ 
        professional data about players
        """
        self.position = position
        self.number = number
        self.team = team
        self.primary = False  # is he a regular member of a team?

    def to_Json():

info_team:

from pymongo import MongoClient
from info_player import Player


class TeamDB:
    def __init___(self, nome, fundacao, federacao):
        self.name = name
        self.foundationData = foundationData
        self.federation = federation

    def initializeDB():
        client = MongoClient('localhost', 27017)
        global base
        base = client.league

    def toMongo():
        """
        receive a player object and save it 
        """
    def playersByPosition():
        """
        query players by position
        """
    def lineup():
        """
        receive a team and return its starting line-up, players with primary = true
        """

1 answer

1


To answer your first question: no, you do not need to convert to JSON. The pymongo module provides the possibility to access Collections and make Mongo queries (e.g.: db.name_da_collection.find({})).

I suggest that the process of accessing the database be this way:

import pymongo

DB_URL = 'mongodb://127.0.0.1:27017/<nome da bd>'

COLLECTION_NAME = '<nome da collection>'

src_db = pymongo.MongoClient(DB_URL).get_database() #src_db agora vai ter acesso à base de dados

collection = src_db.get_collection(COLLECTION_NAME) #collection vai ter agora acesso à collection desejada

From now on just do the queries like you do on Mongo. For example, if you want to go through all the documents of Collection Teams:

for team in src_db.collection.find({}):
    ...

By the same token, if you want to find a player with a certain id(?) you can do it:

player = src.collection.find_one({'id':12345})

I hope I helped, any doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.