First, a little background:
I am trying to check if binary image data is saved in Mongo. Given the following scheme:
var mongoose = require('mongoose') , Schema = mongoose.Schema; var imageSchema = new Schema({ mime: String, bin: { type: Buffer, index: { unique: true }}, uses : [{type: Schema.Types.ObjectId}] }); module.exports = mongoose.model('Image', imageSchema);
... I want to ask if the image exists if it adds the link that my object uses and then updates it. If it is not, I want to create (update) it.
Given that it does not exist, the code below works fine. If so, the following code does not and adds another image document to Mongo. I feel this is probably a comparison problem for type Mongo Buffer vs node Buffer, but I can't figure out how to compare them correctly. Please let me know how to update below! Thanks!
Image.findOneAndUpdate({ mime : contentType, bin : image }, { $pushAll : { uses : [ myObject._id ] } }, { upsert : true }, function(err, image) { if (err) console.log(err);
source share