Reputation:
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.
Upvotes: 1
Views: 568
Reputation: 2885
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}
.
Upvotes: 2