Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:26:21+00:00 2026-06-09T15:26:21+00:00

I have a Mongodb collection and I have created a Python class for documents

  • 0

I have a Mongodb collection and I have created a Python class for documents in the collection. The class has some properties and methods which are not stored with the document. Should I try and store them to make the properties searchable or should I not store them and search the objects in Python?

Here is an example:

# Child
class Child:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    @property
    def parent(self):
        try:
            return Parent(**db.Parents.find_one({'child':self._id}))
        except:
            return None

# Parent
class Parent:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    @property
    def child(self):
        try:
            return Child(**db.Children.find_one({'parent':self._id}))
        except:
            return None

In this example, to search for all the children who’s parent’s name is “foo”, I have to do this:

results = [Child(**c) for c in db.Children.find() if c.parent.name == 'foo']

This means I have to pull all the Children documents from Mongodb and search them. Is it smarter to write the Parent data (or a subset of it) to the Child document, so I can use Mongodb to do the searching?? So my Child class could look like this:

# Child
class Child:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    @property
    def parent_name(self):
        try:
            return db.Parents.find_one({'child':self._id})['name']
        except:
            return None

    def _save(self):
        # something like this to get and save all the properties
        data = {m[0]:getattr(self,m[0]) for m in inspect.getmembers(self)}
        db.Children.find_and_modify({'_id':self._id},{'$set':data},upsert=True)

# search
results = [Child(**c) for c in db.Children.find({'parent_name':'foo'})]

So the search is more efficient, but I think having to keep the Child objects updated could be painful and dangerous. If I change the name of a Parent, I have to also rewrite its children. Feels wrong. Any better ideas???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:26:23+00:00Added an answer on June 9, 2026 at 3:26 pm

    You don’t have to load all Children.

    parent_ids = db.Parents.find({'name': 'foo'}).distinct('_id')
    children = db.Children.find({'parent': {'$in': parent_ids}})
    

    (Also, why do you have both a child field on a parent and a parent field on a child?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MongoDB collection which has a created_at stored in each document. These
created a collection in MongoDB consisting of 11446615 documents. Each document has the following
I have a collection in MongoDB with 20 million documents and an index created
I have a MongoDB collection of documents, each document representing a fish . Users
I have created an inherited document class, see the code below. The documents persists
I have a MongoDB collection with data that was not saved through my Derby
I have 10000 documents in one MongoDB collection. I'd like to update all the
I have mongodb running and using morphia. Having a Collection of BatchData Documents and
Let's assume that I have a collection in mongodb, where all of its documents
I have a collection of articles in MongoDB that has the following structure: {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.