0

my question is more about setting up and adding data. I am making the first project with Mongo DB on VS Code and can not find solution what to do next. Google said Instal Cosmos DB, so I did but what next? Does it talk to Flask already? I am lost, need help from someone who works on Flask and MongoDB on VSCode! Feel like an idiot now!


from flask import Flask
from flask_pymongo import PyMongo


app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://localhost:27017/"
mongo = PyMongo(app)

from cocktails.main.views import main
app.register_blueprint(main)

2
  • If you are using a hosted service like CosmosDB the connection string (MONGO_URI in your example code) would be a cloud URI with credentials rather than localhost: Connect to Cosmos DB. I would look for a tutorial to get started as this is a broader question than just configuring a connection. Also note that CosmosDB is an emulation of MongoDB and does differ in features and behaviour. If you are new to MongoDB I would consider using a service like MongoDB Atlas so all of the expected features are available. Commented Nov 20, 2019 at 21:00
  • Hi @Stennie, I have done that just yesterday and I think that I will switch to Atlas. Will be way easier. It`s hard to be newbie so many questions so many confusions. Thanks for your help :). Commented Nov 22, 2019 at 9:17

1 Answer 1

1

Does it talk to Flask already?

Hi,Patrycja. Quick answer is YES! I follow your description and do the steps in this document1 and document2 to create Flask project which accesses Cosmos DB Mongo API.

My app.py looks like below:

from flask import Flask
from flask_pymongo import PyMongo

app = Flask(__name__)
app.debug = True
app.config["MONGO_URI"] = "******"
mongo = PyMongo(app)
print(mongo.db)

@app.route("/")
def home_page():
    items = mongo.db.test.find()
    for item in items:
        name = item["name"]
        return name

I use Cosmos DB Mongo API, the sample data as below:

enter image description here

Run the command python -m flask run: ,get output:

enter image description here

The key point is "MONGO_URI", it is the connection string:

enter image description here

If you concern about the cost,you could use Cosmos DB Emulator for your test. Surely, as @Stennie mentioned in the comment,cosmos db mongo api only supports partial features of Mongo DB. But if your work is only limited to simple queries, you could use it.Otherwise,please consider using Mongo Atlas.

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

1 Comment

Thanks, @Jay I was away from the keyboard yesterday. Yes, that helped me to understand the logic but your right that the best solution is to use MongoDB Atlas which is way easier. Really appreciate your help and big Thank you.

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.