node.js - Finding a match in an array field -


in image sharing application can create albums , add images them. when image deleted site, should removed album(s) stores references image (name, id).

the thing need find albums has stored image (reference) that's removed.

in route below i've tried far, error on query. i've checked mongodb docs , syntax looks this:

db.collection.find( { field : { $in : array } } ); 

in route field , array has switched places, doesn't seem work.

i appreciate help. in advance!

my models looks following:

var albumschema = new schema({       title             : string,       imagename         : [string], <-- array contains of images names       imageid           : [string] <-- array contains of images id's });  modelobject.albumschema = albumschema; modelobject.album = mongoose.model('album', albumschema);  var imageschema = new schema({     name : string,     size : number,     type : string });  modelobject.imgschema = imgschema; modelobject.image = mongoose.model('image', imgschema); 

the route deleting image:

app.get('/blog/delete/:id', function(req, res){      model.imagepost.findbyid(req.params.id, function (err, blog){          var theimage = blog.name;          if (err) {             console.log(err);             //         }          var query = albummodel.album.find( { imagename: { $in : theimage } } );          query.exec(function (err, albums) {              if (!albums) {                 console.log(err);                 //                  blog.remove(function(err) {                     console.log(err);                     //                 });                  res.redirect('/blogs');             }              else {                 // code removing image(s) in albums                  res.redirect('/blogs');             }         });     }); }); 

db.collection.find( { field : { $in : array } } ); not syntax want. says "find me document field has 1 of list values i'm going give in array."

you want use equality while reaching array.

db.collection.find( { imagename:theimage } )

this says find me document imagename theimage return album (or albums if picture can in more 1 album) contains image in imagename array.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -