Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何声明查询以从mongodb获取数据?_Mongodb - Fatal编程技术网

如何声明查询以从mongodb获取数据?

如何声明查询以从mongodb获取数据?,mongodb,Mongodb,在这段代码中,我试图从存在语言字段(可能为null)的mongodb获取这些数据。因此,在属性中,queryI分配了一个过滤器,该过滤器将用于抓取操作。但问题是,当我初始化query={lang:{$exists:true}}}时,query是字符串数据类型,并且查询必须是字典。当我声明query={lang:{$exists:true}}时,它会说->语法错误。显然是因为就我所知,字典的声明是{'key':'value'}。当我像这样声明query={'lang':'{$exists:true

在这段代码中,我试图从存在语言字段(可能为null)的mongodb获取这些数据。因此,在属性中,
query
I分配了一个过滤器,该过滤器将用于抓取操作。但问题是,当我初始化
query={lang:{$exists:true}}}
时,
query
是字符串数据类型,并且查询必须是字典。当我声明
query={lang:{$exists:true}}
时,它会说->
语法错误
。显然是因为就我所知,字典的声明是
{'key':'value'}
。当我像这样声明
query={'lang':'{$exists:true}}
时,由于
keyError
的原因,它不起作用,因为数据库中有一个名为
lang
的文件

那么,如何声明此查询并将其传递到方法中呢


ps:当我在
Webstorm
终端中使用
query={lang:{$exists:true}}
时,它可以工作,但我目前正在开发集成
ipython
jupyter笔记本电脑,以便我可以使用mongodb中的数据创建图形、图表。我还使用了
pandas
作为
dataframe

您是否尝试了query as query={'lang':{'$exists':True}}???MongoDB中的文档说,语法是{field:{$exists:}是的,但是您应该通过python驱动程序发送该命令。例如,在这里你可以看到适当的代码格式。但是我应该在哪里使用点运算符呢。在数据库中,它就像lang:eng;lng:esp等。您是否尝试使用query as query={'lang':{'$exists':True}}???MongoDB中的文档说,语法是{field:{$exists:}}是的,但您应该通过python驱动程序发送该命令。例如,在这里你可以看到适当的代码格式。但是我应该在哪里使用点运算符呢。在数据库中,它就像lang:eng;液化天然气:esp等。
import pandas as pd
from pymongo import MongoClient
import matplotlib.pyplot as plt


def _connect_mongo(host, port, db):
    """ A util for making a connection to mongo 

    if username and password:
        mongo_uri = 'mongodb://%s:%s@%s:%s/%s' % (username, password, host, port, db)
        conn = MongoClient(mongo_uri)
    else:
    """    
    conn = MongoClient(host, port)


    return conn[db]


def read_mongo(db, collection, host, port, query):
    """ Read from Mongo and Store into DataFrame """

    # Connect to MongoDB
    db = _connect_mongo(host=host, port=port, db=db)

    # Make a query to the specific DB and Collection
    cursor = db[collection].find(query)

    # Expand the cursor and construct the DataFrame
    df =  pd.DataFrame(list(cursor))

    '''
     Delete the _id
    if no_id:
        del df['_id']
    '''
    return df    


#initialization
db = 'twittersmall'
collection='twitterdata'
query='{lang:{$exists: true}}'
host='localhost'
port=27017

var = read_mongo(db, collection, host, port, query)
print var

tweets_by_lang = var['lang'].value_counts()

fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Languages', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 languages', fontsize=15, fontweight='bold')
tweets_by_lang[:5].plot(ax=ax, kind='bar', color='red')