0

I got a flask application running in one vagrant box on (192.168.2.100) and an other vagrant box running mongoDB 3.4 on (192.168.2.110).

When i run this python code from my app:

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'])

It prints all users inserted...

BUT:

When i ssh into my mongodb and run in mongo console:

use database_local
show collections

it returns nothing, like the database is empty.

2
  • Shouldn't you use database app_users instead of database_local when you ssh into the machine? Commented May 31, 2017 at 10:40
  • hey i edited my source-code, but still the same result Commented May 31, 2017 at 10:45

1 Answer 1

1

Your MongoDB related code looks strange... Try to connect like this:

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

And then after ssh-ing into your remote machine, select a proper database:

use database_local
show collections

you should see app_users collection with the data you inserted.

Read more about PyMongo basics here!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.