如何从 python 使用 Heroku 的 mongolab 插件?

发布于 2024-12-27 02:51:29 字数 30 浏览 4 评论 0原文

该文档仅讨论如何从 ruby​​ 执行此操作。

The documentation only talks about how to do it from ruby.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故人的歌 2025-01-03 02:51:29

我是来自 MongoLab 的 Will。我们有一个关于如何使用官方 python 驱动程序 (pymongo) 在 Python 中进行连接的通用示例。这个示例并不是从 Heroku 进行连接,但应该是类似的。不同之处在于,您需要从 Heroku ENV 环境中提取驱动程序配置以提供给驱动程序。

https://github.com/mongolab/mongodb-driver -examples/blob/master/python/pymongo_simple_example.py

如果您仍然遇到问题,请随时直接联系我们 [电子邮件受保护]

-will

This is Will from MongoLab. We have a generic example of how to connect in Python using the official python driver (pymongo). This example is not for connecting from Heroku per say but it should be similar. The difference is that you will need to pluck your driver config from your Heroku ENV environment to supply to the driver.

https://github.com/mongolab/mongodb-driver-examples/blob/master/python/pymongo_simple_example.py

If you still have trouble feel free to contact us directly at [email protected]

-will

爱已欠费 2025-01-03 02:51:29

我正在使用以下内容:

import os
from urlparse import urlsplit
from pymongo import Connection

url = os.getenv('MONGOLAB_URI', 'mongodb://localhost:27017/testdb')
parsed = urlsplit(url)
db_name = parsed.path[1:]

# Get your DB
db = Connection(url)[db_name]

# Authenticate
if '@' in url:
    user, password = parsed.netloc.split('@')[0].split(':')
    db.authenticate(user, password)

I'm using the following:

import os
from urlparse import urlsplit
from pymongo import Connection

url = os.getenv('MONGOLAB_URI', 'mongodb://localhost:27017/testdb')
parsed = urlsplit(url)
db_name = parsed.path[1:]

# Get your DB
db = Connection(url)[db_name]

# Authenticate
if '@' in url:
    user, password = parsed.netloc.split('@')[0].split(':')
    db.authenticate(user, password)
Oo萌小芽oO 2025-01-03 02:51:29

将插件安装到 Heroku 应用后,通过在命令行上运行 heroku config 来获取连接字符串设置。

将有一个带有 MONGOLAB_URI 键的条目,其形式如下:

MONGOLAB_URI => mongodb://user:[电子邮件受保护]:27707/db

只是来自的信息python 中的 uri 通过从 uri 字符串创建连接。

Get the connection string settings by running heroku config on the command line after installed the add-on to your heroku app.

There will be an entry with the key MONGOLAB_URI in this form:

MONGOLAB_URI => mongodb://user:[email protected]:27707/db

Simply the info from the uri in python by creating a connection from the uri string.

寂寞花火° 2025-01-03 02:51:29

我认为这样的事情应该有效:

import os
import sys
import pymongo


mongo_url = os.getenv('MONGOLAB_URI', 'mongodb://localhost:27017')
db_name = 'mongotest'

if __name__ == '__main__':
  try:
   connection = pymongo.Connection(mongo_url)
   if 'localhost' in self.mongo_url:
     db_name = 'my_local_db_name'
   else:
     db_name = self.mongo_url.rsplit('/',1)[1]
   database = connection[db_name]
  except:
   print('Error: Unable to Connect')
   connection = None

if connection is not None:
  database.test.insert({'name': 'foo'})

I think something like this should work:

import os
import sys
import pymongo


mongo_url = os.getenv('MONGOLAB_URI', 'mongodb://localhost:27017')
db_name = 'mongotest'

if __name__ == '__main__':
  try:
   connection = pymongo.Connection(mongo_url)
   if 'localhost' in self.mongo_url:
     db_name = 'my_local_db_name'
   else:
     db_name = self.mongo_url.rsplit('/',1)[1]
   database = connection[db_name]
  except:
   print('Error: Unable to Connect')
   connection = None

if connection is not None:
  database.test.insert({'name': 'foo'})
久而酒知 2025-01-03 02:51:29

PyMongo 现在提供了一个 get_default_database() 方法,使整个练习变得简单:

from pymongo import MongoClient

client = MongoClient(os.environ['MONGOLAB_URI'])
db = client.get_default_database()

PyMongo now provides a get_default_database() method that makes this entire exercise trivial:

from pymongo import MongoClient

client = MongoClient(os.environ['MONGOLAB_URI'])
db = client.get_default_database()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文