简体   繁体   中英

How to select several columns in Pymongo and apply “where” clause?

I have data such as columns = db['WDIData'].find_one().keys() returns:

Out[121]: dict_keys(['_id', 'Country Name', 'Country Code', 'Indicator Name', 'Indicator Code', '1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017'])

Sample: db['WDIData'].find_one()

{'_id': ObjectId('5c42044b90d2b00f248c73bd'),
 'Country Name': 'Arab World',
 'Country Code': 'ARB',
 'Indicator Name': 'Access to electricity (% of population)',
 'Indicator Code': 'EG.ELC.ACCS.ZS',
 '1960': nan,
 '1961': nan,
 '1962': nan,
 '1963': nan,
 '1964': nan,
 '1965': nan,
 '1966': nan,
 '1967': nan,
 '1968': nan,
 '1969': nan,
 '1970': nan,
 '1971': nan,
 '1972': nan,
 '1973': nan,
 '1974': nan,
 '1975': nan,
 '1976': nan,
 '1977': nan,
 '1978': nan,
 '1979': nan,
 '1980': nan,
 '1981': nan,
 '1982': nan,
 '1983': nan,
 '1984': nan,
 '1985': nan,
 '1986': nan,
 '1987': nan,
 '1988': nan,
 '1989': nan,
 '1990': 74.3842390444175,
 '1991': 74.38222008801621,
 '1992': 74.3131602867208,
 '1993': 75.3493248355472,
 '1994': 75.7885216921504,
 '1995': 76.214137971973,
 '1996': 77.20514987247721,
 '1997': 77.5737299371645,
 '1998': 78.39551105118579,
 '1999': 78.9655316665757,
 '2000': 78.7623297831137,
 '2001': 80.1492565068256,
 '2002': 80.3599784304366,
 '2003': 81.35478764291192,
 '2004': 82.66240234337971,
 '2005': 83.6875762079726,
 '2006': 85.8002961272357,
 '2007': 84.7357232408233,
 '2008': 85.4328268149662,
 '2009': 85.1898151220048,
 '2010': 86.1361336464124,
 '2011': 86.782683284186,
 '2012': 87.28824394021481,
 '2013': 88.38970514764809,
 '2014': 88.07677413801581,
 '2015': 88.5179673938546,
 '2016': 88.7686540243445,
 '2017': nan}

What is year:

regex = re.compile("[0-9]{4}")
years = list(filter(regex.search, columns))
year = years[40]

Out[132]: '2000'

data = db['WDIData'].find({}, {'_id': 1, 'Country Name': 1, 'Country Code': 1, 'Indicator Name': 1, 
         'Indicator Code': 1, year: 1})

It works but I don't know how ti use where now. I'm searching for something like this: data = db['WDIData'].find({'_id': 0, 'Country Name': 1, 'Indicator Name': 1, year: 1}).where({'Indicator Name': ind})

But get TypeError: code must be an instance of str. Not sure how to deal with this. It should be intuitive, shouldn't it?

The cursor object does not have a .where method. You need to use the filter argument. See .find documentation.

You need to first select the document using the filter option then project the fields.

db['WDIData'].find(
    {'Indicator Name': ind},
    {
        'Country Name': 1, 
        'Country Code': 1, 
        'Indicator Name': 1, 
        'Indicator Code': 1, 
        'year': 1
    }
)

I found the answer thanks to a given suggestion. It's in .find method. Just need to pass arguments in the right order so they act as where and select:

cur = db['WDIData'].find({'Indicator Name': ind}, {'_id': 0, 'Country Name': 1, 'Country Code': 1, year: 1})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM