0
class Portfolio:

    def read(self, pathfilename):
        .... stuff .... 
        self.portfolio[comp_symbol] = {'name': comp_name , 'holdings': comp_holdings}



    def save_portfolio(self, port_collection):
        port_collection.insert(self.portfolio)


    def list_tickers(self):
        return (self.portfolio.keys())

    def __init__(self):
            self.portfolio = {}
            self.id = None  

Here is how to call it:

    port = Portfolio()
    print "==================================================================================="
    print port.id
    print port.portfolio
    print "==================================================================================="

    port.read(portfolio_file)
    print port.id
    print port.portfolio
    print port.portfolio.keys()
    print "==================================================================================="

    print port.list_tickers()   
    port.save_portfolio(port_collection)
    print port.list_tickers()
    print port.portfolio

The problem is that on performing the insert with pymongo, the property called portfolio changes, and there is an extra key added. For example: print port.list_tickers() is different before and after the insert procedure and I do not see why this should be the case. Before the insert, I get ['CSCO', 'RSA', 'ARO'] and after the insert, I get: ['CSCO', 'RSA', '_id', 'ARO'], but I should still be reading from the same class property. The additional _id is obviously the id from MongoDB.

disruptive
  • 5,687
  • 15
  • 71
  • 135

1 Answers1

1

The _id attribute is mandatory for records in MongoDB - it serves as the unique identifier for a record. MongoDB will create this automatically upon insertion of new records. There is no way to avoid its inclusion in the keys. However, since it's guaranteed to be in each record, you could safely pop it from the list if it really irritates you.

Evan Wyse
  • 36
  • 2