简体   繁体   English

如何在Pymongo中选择几列并应用“ where”子句?

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

I have data such as columns = db['WDIData'].find_one().keys() returns: 我有数据,例如columns = db['WDIData'].find_one().keys()返回:

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() 示例: 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' 出[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. 它有效,但是我不知道现在怎么where使用。 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}) 我正在搜索这样的内容: 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. 但是获取TypeError:代码必须是str的实例。 Not sure how to deal with this. 不确定如何处理。 It should be intuitive, shouldn't it? 它应该直观,不是吗?

The cursor object does not have a .where method. 游标对象没有.where方法。 You need to use the filter argument. 您需要使用filter参数。 See .find documentation. 请参阅.find文档。

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. 使用.find方法。 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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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