0

I have written some codes to store image files in MongoDB. Now I want to filter and retrieve some images from the mongoDB. I want to filter out some images which has some set of characters on the image name. For Ex: say I have stored aaaa_DEX.jpg, bbbb_DEX.jpg, cccc_BVX.jpg, dddd_OUI.jpg, eeee_DEX.jpg images in mongoDB and I want to get all the images which has the "DEX" on there names. Will it be possible with Query builder? How can I do this?

To upload I use:

public JsonResult UploadPrimaryImage(string hotelCode)
{
    var db = _hoteldbObj.Instance();
    var primaryImageBucket = new MongoGridFS(db, new MongoGridFSSettings() {Root = "HotelPrimaryImage"});

    foreach (string httpFile in Request.Files)
    {
        var postedFile = Request.Files[httpFile];

        if(postedFile == null)
            throw new InvalidOperationException("Invalid file");

        var bytes = ReadToEnd(postedFile.InputStream);

        using (var c = primaryImageBucket.Create(hotelCode, new MongoGridFSCreateOptions() { ContentType = postedFile.ContentType }))
        {                    
            c.Write(bytes, 0, bytes.Length);
            c.Flush();
            c.Close();
        }
    }

    return new JsonResult();
}

Thank You

i3arnon
  • 113,022
  • 33
  • 324
  • 344
gamini2006
  • 299
  • 3
  • 17
  • Can you provide a sample document? Also are you using GridFS (http://docs.mongodb.org/manual/applications/gridfs/) to store? – James Wahlin Mar 01 '13 at 17:53
  • I have add the used code to upload files. plz see, Thx – gamini2006 Mar 01 '13 at 18:18
  • 1
    If querying on the full filename then performing a .find("ABC") where ABC is your filename will do the trick. If you want to query on a substring within the file name, my suggestion would be to save the substring as part of the metadata object. See the following for an example of using metadata: http://stackoverflow.com/questions/10281610/query-on-mongodb-gridfs-metadata-java – James Wahlin Mar 01 '13 at 18:36
  • Thank you James. I'll try that and let you know the status. But without modifying the uploading code cant I do with the GT, GTE, In, LT, LTE, Near, NE, And, Or Query operator? Ex: var query = Query.EQ("_id", id); var entity = collection.FindOne(query); link: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/ – gamini2006 Mar 02 '13 at 01:33

1 Answers1

1

Performing a .find("ABC") where ABC is your filename will handle this if querying on the full file name.

If you want to query on a substring within the file name, my suggestion would be to save the substring as part of the metadata object. See this post for an example of working with metadata.

Community
  • 1
  • 1
James Wahlin
  • 2,811
  • 21
  • 22