0
import mongoengine

def global_init():
    mongoengine.register_connection(alias="core", name="project")

class Worker(mongoengine.Document):
    username = mongoengine.StringField()
    salary_per_hour = mongoengine.FloatField()

    total_hours_worked = ********[1]

    meta = {
        "db_alias": "core",
        "collection": "users"
    }

class Project(mongoengine.Document):
    name = mongoengine.StringField()
    associated_users = mongoengine.ListField(ReferenceField(User))
    progress = mongoengine.EmbeddedDocumentField(Progress)

    meta = {
        "db_alias": "core",
        "collection": "projects"
    }

class Contrib(mongoengine.Document):
    project = mongoengine.ReferenceField(Project)
    user = mongoengine.ReferenceField(User)
    hour = mongoengine.FloatField()

    meta = {
        "db_alias": "core",
        "collection": "contribs"
    }

class Progress(mongoengine.EmbeddedDocument):
    total_hours = ********[2]
        percentage = ********[3]
    total_cost = ********[4]

Hi, i'm trying to build a worker-project relationship with mongoengine there will be various projects and workers. Contributions will be main inputs to the program. I need these databases for future functionalities. I want to build some relationships between databases rather than updating everything depending on an input. For example;

[1] total work hour of a worker.

[2] total_hours workers worked on that project.

[3] continuous percentage calculation of project with a percentage reference depending on specified work hour.

[4] cost of project up to now depending on workers salary_per_hour and their work hour

I am new to noSQL databases and I don't know even if I wrote things right. Is it possible to build these relationships between databases with some mapping functions? I don't know what should I use for these or is there any needed changes on code for proper working. Can you write some example or suggest me some sources for studying these things. I was lost in mongoengine documentation for last 2 hours but couldn't meet my needs.

Thanks for your help in advance.

Berkin Anık
  • 133
  • 8

1 Answers1

1

well you wouldn't be using separate database....you would be in one database and potentially have different collections. but based on your 4 key output requirements I would suggest just keep it flat with a single collection - and design your app to put in the fields/data that will satisfy your 4 output requirements...that is the advantage of NoSQL - don't split things apart and then join them - instead just put all the data in together...

you generally don't want to input aggregate sums as that calculation is better done on output so just calculate inputs...just input the granular data in fields/format that will make your aggregate queries easy to run....

Cahaba Data
  • 624
  • 1
  • 4
  • 4