0

recently I've been struggling on how can I list all the metadata only of GridFs collection using mongoose. I've tried using this

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);
}

by the way it's not the full code, it's just a snippet of what I'm trying to do. I can't seem to get the metadata list. Thank you everyone.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Alleo Indong
  • 327
  • 3
  • 13
  • If it's not an [MCVE](https://stackoverflow.com/help/mcve) you might have a harder time getting people to help. – George Hilliard Jul 01 '14 at 03:13
  • I just want to know how to query the metadata of gridfs using mongoose because `this.store = mongoose.model('file.files', this.definition);` can't seem to get the collection – Alleo Indong Jul 01 '14 at 03:27

1 Answers1

7

Not a really good idea to define a strictSchema for this. The GridFS spec allows fairly loose interaction with metadata fields, so those methods to insert are not going to use a "schema", and therefore it makes sense not to be strict with the schema you define.

You can do something like this though:

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 );

});

Where fs.files would be the "default" collection name unless you named it otherwise. Essentially the call to model just "binds" the third argument as the specifically named collection.

Since GridFS collections are really just plain collections, you can just query with the standard methods.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • thank you for the tip about the schema, I've tried your format of var Grid = mongoose.model("Grid", gridSchema, "fs.files" ); replacing fs.files with my file.files and the result is still empty([]) but I've got data on the collection as i see on the rockmongo – Alleo Indong Jul 01 '14 at 04:11
  • @AlleoIndong I have also run this myself before posting. Are you sure you are pointing your mongoose connection to the correct database? Possibly your GridFS collections are not in the same database as the one your general application is using. – Neil Lunn Jul 01 '14 at 04:26
  • yes I am pointing it with the correct connection, maybe I have mistakes on some other areas, will scan through it. Thank you so much – Alleo Indong Jul 01 '14 at 05:52