如何使用python 3.4.3在mongodb中插入值

时间:2016-03-15 11:57:42

标签: python mongodb python-3.x pymongo

我写了以下代码&我在网上搜索过..但是我没有得到任何东西,我已经尝试了一切,但仍然得到同样的错误。只是无法理解这个错误,任何人都可以帮助我。我真的需要帮助。

    from pymongo import MongoClient, collection
    c = MongoClient("localhost" , 27017 , connect = True)
    c.test_database
    from datetime import datetime
    db = c.name
    result = db.restaurants.insert_one(
          {
           "address": {
               "street": "2 Avenue",
               "zipcode": "10075",
                "building": "1480",
               "coord": [-73.9557413, 40.7720266]
    },
         "borough": "Manhattan",
         "cuisine": "Italian",
          "grades": [
             {
                "date": datetime.strptime("2014-10-01", "%Y-%m-%d"),
                "grade": "A",
                "score": 11
        },
        {
            "date": datetime.strptime("2014-01-16", "%Y-%m-%d"),
            "grade": "B",
            "score": 17
        }
    ],
       "name": "Vella",
       "restaurant_id": "41704620"
})result.inserted_id

这是我的错误,我无法理解

    Traceback (most recent call last):
           File "E:\practice\db.py", line 29, in <module>
          "restaurant_id": "41704620"
          File "C:\Python34\lib\site-packages\pymongo\collection.py", line 622, in insert_one
        with self._socket_for_writes() as sock_info:
       File "C:\Python34\lib\contextlib.py", line 59, in __enter__
       return next(self.gen)
      File "C:\Python34\lib\site-packages\pymongo\mongo_client.py", line 712, in _get_socket
      server = self._get_topology().select_server(selector)
      File "C:\Python34\lib\site-packages\pymongo\topology.py", line 142, in select_server
address))
      File "C:\Python34\lib\site-packages\pymongo\topology.py", line 118, in select_servers
      self._error_message(selector))
      pymongo.errors.ServerSelectionTimeoutError: localhost:27017:
          [WinError 10061] No connection could be made because the target machine actively refused it

如果有人知道,请帮助我。

0 个答案:

没有答案