【问题标题】:Pymongo data saved by app is not available in mongo shell应用程序保存的 Pymongo 数据在 mongo shell 中不可用
【发布时间】:2020-11-01 02:40:34
【问题描述】:

我正在使用 pymongo 构建一个烧瓶应用程序。在我的 python 脚本中,数据按应有的方式使用 .save() 保存,并且可以按应有的方式使用 .find() 进行检索,并且如果我重新启动应用程序,它也会持续存在。但是,如果我从另一个终端或在停止应用程序后从同一终端检查 mongo 数据库,则数据不存在。为了确保我正确使用 pymongo,我从 w3schools.com 复制并粘贴了以下基本脚本:

#!/usr/bin/python3
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway 37" }
obj = mycol.insert_one(mydict)
obj = mycol.find_one()
print(obj)

输出是:

{'_id': ObjectId('5f09b0c4dfedb570e2f5411e'), 'name': 'John', 'address': 'Highway 37'}

正如您所料。这是在运行上述脚本之前显示数据库的结果:

> show databases
admin   0.000GB
config  0.000GB
local   0.000GB

这是运行脚本后运行 show databases 的结果:

> show databases
admin   0.000GB
config  0.000GB
local   0.000GB
mydatabase  0.000GB

但是

use mydatabase
db.mydatabase.customers.find()
>

要么这些集合没有保存在我尝试连接的 mongo 服务器中,要么由于某种原因它们没有显示在 shell 中。这是我的 /etc/mongod.config 文件: (注释行已删除,因此 stackoverflow 不会将其格式化为标记)

storage:
  dbPath: /var/lib/mongodb
  journal:
   enabled: true


systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 27017
  bindIp: 127.0.0.1

processManagement:
  timeZoneInfo: /usr/share/zoneinfo

我不确定这些是否相关,但是当我进入 mongo shell 时总是有启动警告:

MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("20a57a18-201c-4393-a9a3-c0ee53e028e8") }
MongoDB server version: 4.2.8
Server has startup warnings: 
2020-07-11T12:16:02.034+0000 I  STORAGE  [initandlisten] 
2020-07-11T12:16:02.034+0000 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-07-11T12:16:02.034+0000 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-07-11T12:16:02.940+0000 I  CONTROL  [initandlisten] 
2020-07-11T12:16:02.940+0000 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-07-11T12:16:02.940+0000 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-07-11T12:16:02.940+0000 I  CONTROL  [initandlisten]

在看到“连接到:mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb”时,我尝试将 MongoClient 连接更改为“mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName= mongodb”只是为了看看这是否可行,但它只是抛出错误:

    /home/vagrant/.local/lib/python3.6/site-packages/pymongo/compression_support.py:55: UserWarning: Unsupported compressor: disabled
  warnings.warn("Unsupported compressor: %s" % (compressor,))
/home/vagrant/.local/lib/python3.6/site-packages/pymongo/common.py:756: UserWarning: Unknown option gssapiservicename
  warnings.warn(str(exc))

【问题讨论】:

    标签: python mongodb flask pymongo persistence


    【解决方案1】:

    你想要的syntax是:

    db.customers.find()
    

    在外壳上。您已经使用use 命令指定了数据库。

    要列出集合,它是:

    show collections
    

    【讨论】:

    • 这是我的问题,谢谢!!不用再拉我的头发了! :)
    • 对不起!平台新手。
    猜你喜欢
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多