2017-05-31 79 views
0

我得到了一个烧瓶应用程序运行在(192.168.2.100)上的一个流浪箱和运行mongoDB 3.4(192.168.2.110)上的其他流浪病箱。Python烧瓶MongoDB - 收集没有数据

当我运行从我的应用程序这个Python代码:

mongo_client = MongoClient(mongodb://myuser:[email protected]/database_local) 
g.db = mongo_client.db.database_local 

app_users = g.db.app_users 

user_id = app_users.insert_one(
    { 
     'username': 'user', 
     'password': 'password' 
    } 
).inserted_id 
// user_id is created and returned ("592b2c57962d7408027e274d") 

// When i run this to verify: 
users = g.db.app_users.find() 
for user in users: 
    print(user['username']) 

它打印所有用户插入...

但是:

当我ssh到我的MongoDB和Mongo的控制台中运行:

use database_local 
show collections 

它什么也没有返回,就像数据库是空的一样。

+0

你不应该使用数据库'app_users'代替database_local'的'当你ssh到机器? – errata

+0

嘿我编辑我的源代码,但仍然是相同的结果 –

回答

1

你的MongoDB相关的代码看起来很奇怪...尝试连接这样的:

client = MongoClient('mongodb://myuser:[email protected]/') # pass just these options 

db = client.database_local # select database_local database 
user_id = db.app_users.insert_one({ # insert into app_users collection 
    'username': 'user', 
    'password': 'password' 
}).inserted_id 

再经过-ING SSH到您的远程机器,选择合适的数据库:

use database_local 
show collections 

你应使用您插入的数据查看app_users集合。

Read more about PyMongo basics here!

+0

工程伟大thnaks :) –

+0

非常欢迎您! ;) – errata