【问题标题】:List all GridFs metadata only using mongoose on nodejs仅在 nodejs 上使用 mongoose 列出所有 GridFs 元数据
【发布时间】:2014-07-01 02:51:17
【问题描述】:

最近我一直在苦苦思索如何使用 mongoose 仅​​列出 GridFs 集合的所有元数据。我试过用这个

var mongoose    = require('mongoose');
public.schema = {
    filename    : { type: String, required: true },
    contentType : { type: String, required: true },
    length      : { type: Number, required: true },
    chunkSize   : { type: Number, required: true },
    uploadDate  : { type: Date, required: true },
    aliases     : { type: String, required: false },
    metadata    : { type: String, required: false },
    md5         : { type: String, required: true }
};

var schema = mongoose.Schema;
this.store = mongoose.model('file.files', this.definition);

public.find = function(query) {
    return this.store.find(query);
}

顺便说一句,它不是完整的代码,它只是我正在尝试做的一个 sn-p。我似乎无法获取元数据列表。谢谢大家。

【问题讨论】:

  • 如果不是MCVE,您可能很难让人们提供帮助。
  • 我只是想知道如何使用猫鼬查询gridfs的元数据,因为this.store = mongoose.model('file.files', this.definition);似乎无法获取集合

标签: javascript node.js mongodb mongoose gridfs


【解决方案1】:

为此定义一个 strictSchema 并不是一个好主意。 GridFS 规范允许与元数据字段进行相当松散的交互,因此这些插入方法不会使用“模式”,因此不必严格使用您定义的模式。

你可以这样做:

var mongoose = require("mongoose"),
    Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/test');

var gridSchema = new Schema({},{ strict: false });

var Grid = mongoose.model("Grid", gridSchema, "fs.files" );

Grid.find({},function(err,gridfiles) {

  if (err) throw err;
  console.log( gridfiles );

});

除非您以其他方式命名,否则fs.files 将是“默认”集合名称。本质上,对模型的调用只是将第三个参数“绑定”为特定命名的集合。

由于 GridFS 集合实际上只是普通集合,因此您可以使用标准方法进行查询。

【讨论】:

  • 感谢您提供有关架构的提示,我尝试了您的 var Grid = mongoose.model("Grid", gridSchema, "fs.files" ); 格式用我的 file.files 替换 fs.files ,结果仍然是空的([]),但我在 rockmongo 上看到了关于集合的数据
  • @AlleoIndong 在发布之前我自己也运行过这个。您确定将猫鼬连接指向正确的数据库吗?可能您的 GridFS 集合与您的一般应用程序使用的数据库不在同一个数据库中。
  • 是的,我用正确的连接指向它,也许我在其他一些地方有错误,将扫描它。非常感谢
猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 2015-01-25
  • 2013-01-27
  • 2018-03-24
  • 2022-01-20
  • 2016-04-25
  • 2015-11-22
  • 2020-05-16
相关资源
最近更新 更多