preston
preston

Reputation: 4367

Meteor JS CollectionFS and/or MongoDB Query: How to Query CollectionFS For a Document

This is the code I used to initialise my CollectionFS:

Uploads = new FS.Collection('uploads', {
  stores: [new FS.Store.FileSystem('uploads', {path: '~/projectUploads'})]

});

This is my CollectionFS document I've inserted as shown in Robomongo:

{
    "_id" : "n3M8geaZXnNkZ7mHP",
    "original" : {
        "name" : "AguaBendita.jpg",
        "updatedAt" : ISODate("2014-02-19T11:05:40.000Z"),
        "size" : 73719,
        "type" : "image/jpeg"
    },
    "uploadedAt" : ISODate("2015-04-04T09:24:49.433Z"),
    "copies" : {
        "uploads" : {
            "name" : "AguaBendita.jpg",
            "type" : "image/jpeg",
            "size" : 73719,
            "key" : "uploads-n3M8geaZXnNkZ7mHP-AguaBendita.jpg",
            "updatedAt" : ISODate("2015-04-04T09:24:49.000Z"),
            "createdAt" : ISODate("2015-04-04T09:24:49.000Z")
        }
    }
}

I know how to find or findOne the document based on the _id e.g.

var testingfetchAgua = Uploads.find({_id: 'n3M8geaZXnNkZ7mHP'}).fetch();

or

var testingfetchAgua = Uploads.files.findOne({_id: 'n3M8geaZXnNkZ7mHP'}).fetch();

however I don't know how to find or findOne the document based on the "name" key/value inside the "original" key/value?

Is this even possible in CollectionFS?

How does one do that in a CollectionFS query?

How do you do that in a MongoDB query?

Is the query in MongoDB the same in CollectionFS?

Upvotes: 1

Views: 320

Answers (1)

Kirill
Kirill

Reputation: 66

  • It is possible.

  • You do it with a dot notation like so: var testingfetchAgua = Uploads.find({ 'original.name': 'AguaBendita.jpg' }).fetch();

  • AFAIK queries are the same, since CollectionFS uses MongoDB.

Here's also a related question. And here's MongoDB docs, which you can consult when facing such issues.

Edit (to expand a bit on the relation of FS and Mongo):

When you call Uploads.files you actually get the underlying MongoDB Collection.

An FS.Collection provides a collection in which information about files can be stored. It is backed by an underlying normal Mongo.Collection instance. Most collection methods, such as find and insert are available on the FS.Collection instance. If you need to call other collection methods such as _ensureIndex, you can call them directly on the underlying Mongo.Collection instance available through myFSCollection.files.

From the CollectionFS github readme

Upvotes: 0

Related Questions