How to integrate and make queries with pymongo?

1

I'm learning to integrate Python and MongoDB, and because of that, I set myself a challenge: to 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, as well as talking to the bank.

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

My questions are: do I need to convert the generated objects to Json (thought to create a to_json method)? 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
        """
    
asked by anonymous 07.11.2017 / 12:06

1 answer

1

Answering 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 (eg db.collection.find ()).

I suggest that the process of accessing the database is 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 make the queries as you do in Mongo. For example, if you want to go through all of the collection Teams documents:

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:

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

I hope to have helped, any doubts it has.

    
08.11.2017 / 16:29