1

I am trying to put together a program that wil parse through mongodb and get each page (eventually I will want to graph it) but I don't know what I am doing wrong here. I have been through the tutorial http://api.mongodb.org/python/2.0/tutorial.html but it I don't know how to query dynamically (looping through). My guess is that it has something to do with results being returned in unicode but i'm not sure. Here is my code.

import pymonogo
from pymongo import Connection
c = Connection()
dbs = c.database_names()
for db in dbs:
  print db
  for col in c[db].collection_names():
    print '\t', col
    for pag in c[db].col.find():
      print pag

I'm just doing some analytics on an existing databases. (I have about 5 dbs that each have 1-20 collections, each collection has from 0 - 1500 pages. I am hoping to graph the pages, but I haven't gotten far enough into the graphing library yet to see exactly how that will work out.

If you can help thanks.

4

1 回答 1

2

如果您只想从所有数据库和所有集合中获取所有数据,那么您的代码几乎是正确的。这是带有小修复的代码(c[db][col]而不是c[db].col):

from pymongo import MongoClient

c = MongoClient()
dbs = c.database_names()
for db in dbs:
    print db
    for col in c[db].collection_names():
        print '\t', col
        for pag in c[db][col].find():
            print pag
于 2013-04-30T17:14:08.593 回答