As a preface, I'm using Node.js with Mongo-db-native.
I'm also using GridFS to store images and each image has meta data, one of which is a Product Id.
I want to query all the the fs.files
and return images that are associated to a specific product.
Here's how I am currently doing this:
this.collection.ensureIndex({
product_id: 1,
}, function (err, edIndex) {
self.collection.group( ['group'] , {
"product_id": ObjectID(product_id)
} , {
docs: []
} , function (doc, prev) {
prev.docs.push({
width: doc.width,
height: doc.height,
_id: doc._id
});
} , true , function (err, results) {
if (err) {
callback(err)
} else {
callback(null, results)
}
});
});
I'm finding that this is extremely slow. Does anyone have any suggestions as an alternative or how to increase the performance of this?
Thank you!
Here is a simple query in Mongo shell syntax which will find all GridFS files with ProductId = 42 in its metadata.
db.fs.files.find({"metadata.ProductId": 42});
The returned documents will contain a filename which can be used with mongo-native's GridStore.