1

I have a MySQL database with a table called sources, there I have two columns called srctype and url where srctype is a name (e.g: Hello) and url is an url (e.g: http://google.com)

In Python using SQLAlchemy, I could filter the srctype and get a list of the urls, for example;

src = "hello"    
links = session.query(sources).filter_by(srctype=src).all()

Easy, now I am migrating this data to a MongoDB, for this I am using pymongo.

I have a function where it saves the srctype and url to mongodb's database

    def insertSources(self, srctype, link):
        """ Insert rss sources so we can parse them """
        new = {srctype: link}
        self.__sources.insert(new)

and a function that retrieves the srctype

 def getSources(self, type=None, single=True): # type == srctype
    if type:
        if single:
            return self.__sources.find_one()
        else:
            return iter(self.__sources.find({srctype:type}))
    else:
        if single:
            return self.__sources.find_one()
        else:
            return iter(self.__sources.find())

The problem here is, since there is no column called srctype and no column called url, how can I do the same as the SQLAlchemy/MySQL example?

In MySQL would be;

SELECT * FROM sources WHERE srctype="hello"

I wonder how will that be in the retrieve function I have (also in the insert function, since I am not sure if what I did there is correct for the job I want). In the insertSources function I simple add a dict to MongoDB, clearly I won't be able to get the srctype in the getSources function, since srctype in MongoDB is no column. Any help would be great.

2
  • 1
    Why would you want to migrate data that you want to access in an SQL-like manner to a NoSQL database? Is this just an exercise to work with Mongo? Commented Nov 2, 2012 at 21:34
  • Unfortunately not, this is a real job where I was asked to do this, but then I got hooked up with this problem. Commented Nov 2, 2012 at 21:36

1 Answer 1

2

Your problem is that you are not setting the name correctly when saving the data. Instead of

def insertSources(self, srctype, link):
    """ Insert rss sources so we can parse them """
    new = {srctype: link}
    self.__sources.insert(new)

You should do

def insertSources(self, srctype, link):
    """ Insert rss sources so we can parse them """
    new = {'srctype': srctype, 'link': link}
    self.__sources.insert(new)

And similarly in getSources(). If srctype is passed, find() and find_one() should receive {'srctype': type}.

Sign up to request clarification or add additional context in comments.

3 Comments

This is indeed what I thought before, but the each site has a different srctype, that's why the SQL example, column -> srctype -> different names; column url -> different links -> fetch the whole row
Did the fix, but I wonder, is this a "quick" fix? Is there such a thing like "columns" in MongoDB? Or your example is the way? Cheers!
I think you need to stop thinking of columns, and start thinking of keys. MongoDB essentially uses dictionaries instead of named tuples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.