最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】远程的mongoDB中GridFS报错:AttributeError GridFS object has no attribute totalSize

MongoDB crifan 2922浏览 0评论

折腾:

【已解决】把本地的音频字幕等数据存储到远程服务器的MongoDB数据库中

期间,之前连接本地Mongo的gridfs时可以正常运行的代码:

mongoClient = MongoClient()

gridfsDb = mongoClient.gridfs

fsCollection = GridFS(gridfsDb)

logging.info("fsCollection=%s", fsCollection)

# logging.info("fsCollection.stats()=%s", fsCollection.stats())

logging.info("fsCollection.totalSize()=%s", fsCollection.totalSize())

结果换成去连接到远程的mongo中的gridfs时,竟然就报错了:

    logging.info("fsCollection.totalSize()=%s", fsCollection.totalSize())

AttributeError: ‘GridFS’ object has no attribute ‘totalSize’

现在去连接远程:

mongoClient = MongoClient(

    host="x.x.x.x",

    port=27017,

    username="username",

    password="pwd"

)

看看效果:

代码中出错:

2018/04/02 12:00:54 LINE 175  INFO    fsCollection=<gridfs.GridFS object at 0x10d704190>

Traceback (most recent call last):

  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1668, in <module>

    main()

  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1662, in main

    globals = debugger.run(setup[‘file’], None, None, is_module)

  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1072, in run

    pydev_imports.execfile(file, globals, locals)  # execute the script

  File "/Users/crifan/dev/dev_root/company/naturling/projects/mongodb/saveLocalData/pymongoTest.py", line 178, in <module>

    logging.info("fsCollection.totalSize()=%s", fsCollection.totalSize())

AttributeError: ‘GridFS’ object has no attribute ‘totalSize’

class MongoClient(common.BaseObject):

    HOST = "localhost"

    PORT = 27017

    # Define order to retrieve options from ClientOptions for __repr__.

    # No host/port; these are retrieved from TopologySettings.

    _constructor_args = (‘document_class’, ‘tz_aware’, ‘connect’)

    def __init__(

            self,

            host=None,

            port=None,

            document_class=dict,

            tz_aware=None,

            connect=None,

            **kwargs):

        """Client for a MongoDB instance, a replica set, or a set of mongoses.

好像没有username和password?

pymongo  AttributeError GridFS object has no attribute totalSize

python – "AttributeError: ‘GridFS’ object has no attribute ‘find’" – Stack Overflow

python – Pymongo GridFS put Type/Attribute errors – Stack Overflow

难道是:

服务器上的MongoDB的3.2的版本 没有这个totalSize属性?

pymongo    GridFS  totalSize

但是看调试信息,貌似是连接上的:

然后去保存一个文件:

[root@naturling-general-01 nickjr.com]# mongofiles -d gridfs put "nickjr_detail.py" –local "/root/naturling_20180101/crawler/origin_data/nickjr.com/nickjr_detail.py"

2018-04-02T13:31:49.956+0800    connected to: localhost

added file: nickjr_detail.py

[root@naturling-general-01 nickjr.com]# mongo

MongoDB shell version: 3.2.19

connecting to: test

Server has startup warnings:

2018-04-02T11:07:09.596+0800 I CONTROL  [initandlisten]

2018-04-02T11:07:09.596+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.

2018-04-02T11:07:09.596+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never’

2018-04-02T11:07:09.597+0800 I CONTROL  [initandlisten]

2018-04-02T11:07:09.597+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.

2018-04-02T11:07:09.597+0800 I CONTROL  [initandlisten] **        We suggest setting it to ‘never’

2018-04-02T11:07:09.597+0800 I CONTROL  [initandlisten]

2018-04-02T11:07:09.597+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.

2018-04-02T11:07:09.597+0800 I CONTROL  [initandlisten]

> use gridfs

switched to db gridfs

> show collections

fs.chunks

fs.files

> db.fs.files.find().pretty()

{

        "_id" : ObjectId("5ac1c04557511d060d5d6774"),

        "chunkSize" : 261120,

        "uploadDate" : ISODate("2018-04-02T05:31:49.958Z"),

        "length" : 4884,

        "md5" : "4c55ab6a0ecb398ffbe7819b37643c74",

        "filename" : "nickjr_detail.py"

}

>

用于调试去删除时,是否能找到文件:

果然是可以找到并删除文件的:

-》gridfs中的其他操作:

find文件,delete文件 都是正常的

-》证明了:在线的MongoDB 3.2中的gridfs中是没有totalSize这个属性的

-〉再去找找api文档看看

db.collection.totalSize() — MongoDB Manual 3.6

-》

db.collection.totalSize() — MongoDB Manual 3.2

好像也是有的啊

怪了

mongo shell totalSize

db.collection.totalSize() – MongoDB shell method – w3resource

MongoDB – totalSize of a collection – Stack Overflow

[SERVER-16848] Shell’s db.collection.totalSize() returns NAN on latest – MongoDB

难道是因为:远程gridfs中没有任何文件?所以没法获取totalSize?所以报错?

去试了试,确保远程gridfs中有文件:

> use gridfs

switched to db gridfs

> db.fs.files.find().pretty()

{

        "_id" : ObjectId("5ac1c20f57511d061ef265f7"),

        "chunkSize" : 261120,

        "uploadDate" : ISODate("2018-04-02T05:39:27.041Z"),

        "length" : 4884,

        "md5" : "4c55ab6a0ecb398ffbe7819b37643c74",

        "filename" : "nickjr_detail.py"

}

>

但是结果错误依旧

换回本地mongo数据库,看看是否正常了

结果发现此处也不正常了:

    logging.info("fsCollection.totalSize()=%s", fsCollection.totalSize())

AttributeError: ‘GridFS’ object has no attribute ‘totalSize’

错误一样的。

那为何之前代码可以正常运行?

去找找之前是怎么回事。

去看了之前的:

【已解决】MongoDB的GridFS的所有文件的总大小

果然发现是之前就写了:

“然后去Python中的gridfs中去试试:

logging.info("fsCollection.stats()=%s", fsCollection.stats())

logging.info("fsCollection.totalSize()=%s", fsCollection.totalSize())

真的是没有:

    logging.info("fsCollection.stats()=%s", fsCollection.stats())

AttributeError: ‘GridFS’ object has no attribute ‘stats’

    logging.info("fsCollection.totalSize()=%s", fsCollection.totalSize())

AttributeError: ‘GridFS’ object has no attribute ‘totalSize’

而只是:

mongo的shell中,可以用命令:

db.fs.files.totalSize()

或:

db.fs.chunks.totalSize()

查看到总大小

-》pymongo中gridfs并没有实现totalSize这个属性的。

【总结】

mongo的shell中可以通过:

db.fs.files.totalSize()

或:

db.fs.chunks.totalSize()

查看到此处的gridfs这个collection的总大小

但是此处Python代码中用到的是MongoDB的Driver:PyMongo

其中的gridfs这个模块,并没有实现totalSize这个属性,所以代码中调用:

fsCollection.totalSize()

才会报错:

AttributeError: ‘GridFS’ object has no attribute ‘totalSize’

解决办法:

不用这个办法。Python代码中,暂时也不需要这行代码。

非要计算总大小,那再去想办法,比如循环计算单个文件大小加起来,也是可以的。

等有这个需求了再说。

转载请注明:在路上 » 【已解决】远程的mongoDB中GridFS报错:AttributeError GridFS object has no attribute totalSize

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.184 seconds, using 22.19MB memory