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

【已解决】MongoDB的GridFS中只返回file的chunks的个数而不返回chunks.data

file crifan 916浏览 0评论
折腾:
【已解决】把本地的音频字幕等数据存储到本地MongoDB数据库中
期间,由于用:
> db.fs.chunks.find({files_id: ObjectId("5abb397ca4bc71fc7d71c7bd")})
会返回chunk的data,导致数据太多,刷屏太多,看不清个数了:
所以需要想办法去:
如果查询一个file下面有多少个chunks,只返回个数,或者是个数+id,不带chunks.data,以方便查看。
mongodb list count of gridfs file chunks
mongodb gridfs file chunks count
mongodb command show count
[SERVER-20126] Count on GridFS fs.files and fs.chunks slow – MongoDB
count — MongoDB Manual 3.6
mongodb – How to count the number of databases in mongod by mongo shell? – Stack Overflow
去试试length
> db.fs.chunks.find({files_id: ObjectId("5abb397ca4bc71fc7d71c7bd")}).length
function () {
    return this.toArray().length;
}
> db.fs.chunks.find({files_id: ObjectId("5abb397ca4bc71fc7d71c7bd")}).length()
32
Difference between count() and find().count() in MongoDB – Stack Overflow
提到的count也是可以的:
> db.fs.chunks.find({files_id: ObjectId("5abb397ca4bc71fc7d71c7bd")}).count
function (applySkipLimit) {
    var cmd = this._convertToCountCmd(applySkipLimit);

    var res = this._db.runReadCommand(cmd);
    if (res && res.n != null)
        return res.n;
    throw _getErrorWithCode(res, "count failed: " + tojson(res));
}
> db.fs.chunks.find({files_id: ObjectId("5abb397ca4bc71fc7d71c7bd")}).count()
32
【总结】
此处可以通过:
db.fs.chunks.find({files_id: ObjectId("5abb397ca4bc71fc7d71c7bd")}).length()
或:
db.fs.chunks.find({files_id: ObjectId("5abb397ca4bc71fc7d71c7bd")}).count()
都可以查看到:
find出来的结果的个数。

转载请注明:在路上 » 【已解决】MongoDB的GridFS中只返回file的chunks的个数而不返回chunks.data

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
80 queries in 0.198 seconds, using 22.11MB memory