about attachments in RavenDB

535 views
Skip to first unread message

Karuna V

unread,
Apr 1, 2011, 8:32:20 AM4/1/11
to ravendb
Ho can i add images as attachments to already created documents?

I have created documents as products and categories.
No, I want to add images to those products through .net code.

Please send me the code.

Ayende Rahien

unread,
Apr 1, 2011, 8:43:13 AM4/1/11
to ravendb, Karuna V
session.Advanced.DatabaseComamnds.PutAttachment
session.Advanced.DatabaseComamnds.GetAttachment
session.Advanced.DatabaseComamnds.DeleteAttachment

Karuna V

unread,
Apr 1, 2011, 8:52:05 AM4/1/11
to ravendb
what are the arguments that I need to pass?

string key, GUID, byte[],linq.JObject ??

Can u please let me in know in detail with example
I am new to raven DB

Ayende Rahien

unread,
Apr 1, 2011, 8:56:34 AM4/1/11
to ravendb, Karuna V
string Key - the name of the attachment
Guid etag - used to handle concurrency, you can usually set it to null
data byte[] - the content of the attachment
JObject metadata - additional metadata about the attachment

var attachment = documentStore.DatabaseCommands.GetAttachment("ayende");
Assert.Null(attachment);

documentStore.DatabaseCommands.PutAttachment("ayende", null, new byte[] { 1, 2, 3 }, new JObject(new JProperty("Hello", "World")));

attachment = documentStore.DatabaseCommands.GetAttachment("ayende");
Assert.NotNull(attachment);

Assert.Equal(new byte[]{1,2,3}, attachment.Data);
Assert.Equal("World", attachment.Metadata.Value<string>("Hello"));

documentStore.DatabaseCommands.DeleteAttachment("ayende", null);

attachment = documentStore.DatabaseCommands.GetAttachment("ayende");
Assert.Null(attachment);

Karuna V

unread,
Apr 1, 2011, 9:11:42 AM4/1/11
to ravendb
That is very clear. Thank you.

But, how can I say save this image to particular product?
I mean, if I want to display respective images for products in future.
How can I relate the images and products?

Ayende Rahien

unread,
Apr 1, 2011, 9:16:30 AM4/1/11
to ravendb, Karuna V
You might want to look here: http://ravendb.net/bundles/cascade-delete

The document basically owns the picture, and you can reference the picture from the document via a property that holds the picture id.

Karuna V

unread,
Apr 5, 2011, 3:47:19 AM4/5/11
to ravendb
Thanks Ayende.

But where can I see the attachment in Raven studio?
The code was successful but couldn't locate the attachment in the
Raven studio.

Ayende Rahien

unread,
Apr 5, 2011, 3:55:57 AM4/5/11
to ravendb, Karuna V
We aren't showing attachments in the studio currently.
You can look at them in the /static endpoint

Karuna V

unread,
Apr 5, 2011, 4:54:00 AM4/5/11
to ravendb
I have created 680 documents in RavenDB

650 products and 30 categories

I have linked category ids in product documents

Now, how can I get products linked to certain categopries through
query?
Search criteria are name of the product, unitprice, grape, type and
region where grape,type and region are categories

Can you please send me the query to get these results.

sample data:
category1 --
{
"Name": "Cabernet franc",
"CategoryType": "grape",
"CreatedOn": "\/Date(1301498542559)\/",
"CreatedBy": "Importer",
"ModifiedOn": "\/Date(1301498542559)\/",
"ModifiedBy": "Importer"
}

category2 --
{
"Name": "Alsace",
"CategoryType": "region",
"CreatedOn": "\/Date(1301497754168)\/",
"CreatedBy": "Importer",
"ModifiedOn": "\/Date(1301497754168)\/",
"ModifiedBy": "Importer"
}
category3 --
{
"Name": "VDQS",
"CategoryType": "type",
"CreatedOn": "\/Date(1301497754181)\/",
"CreatedBy": "Importer",
"ModifiedOn": "\/Date(1301497754181)\/",
"ModifiedBy": "Importer"
}
product1 --
{
"Name": "wine 1",
"Year": 0,
"Stock": 0,
"UnitPrice": 10.0,
"Volume": "0.75 l",
"IsPromo": false,
"IsPalletWine": false,
"Categories": {
"$type": "System.Collections.Generic.List`1[[System.String,
mscorlib]], mscorlib",
"$values": [
"categories/1",
"categories/2",
"categories/3"
]
},
"CreatedOn": "\/Date(1301498542250)\/",
"CreatedBy": "Importer",
"ModifiedOn": "\/Date(1301498542250)\/",
"ModifiedBy": "Importer"

Ayende Rahien

unread,
Apr 5, 2011, 5:12:19 AM4/5/11
to ravendb, Karuna V
session.Query<Product>()
   .Where(p=>p.Categories.Any( c=> c=="categories/1")
   .ToList();

Karuna V

unread,
Apr 5, 2011, 5:30:31 AM4/5/11
to ravendb
This isn't getting results satisfying all my search criterias.

I have written sql query for my requirement.

select * from products where name='' and unitPrice='' and categories
in (select id from categories where (categorytype='grape'
and name='') and (categorytype='region' and name='')and category
type='type' and name='');

Can you please let me know how can i achieve this in Raven DB?
Your help is much appreciated.

my sample code:
IRepository<Product> repositoryP = new Repository<Product>(_session);
IRepository<Category> repositoryC = new
Repository<Category>(_session);

var resultsP = repositoryP.FindBySpec("productResult", (p =>
p.UnitPrice== 30 && p.Categories.All(q=>Categories.Contains(q))));
or
var resultsP = repositoryP.FindBySpec("productResult", (p =>
p.UnitPrice == 30 && p.Categories.Any(c => c == "categories/1")));
or
var resultsP = repositoryP.FindBySpec("productResult", (p =>
p.UnitPrice == 30 && p.Categories.ToList().All(q =>
Categories.Contains(q))));

Ayende Rahien

unread,
Apr 5, 2011, 5:37:24 AM4/5/11
to rav...@googlegroups.com, Karuna V
You cannot query on a related object using Raven.

Karuna V

unread,
Apr 5, 2011, 8:04:24 AM4/5/11
to ravendb
byte[] result = IDocumentStore.DatabaseCommands.GetAttachment("product/
1");

does this statement returns byte?
I have already stored an image as attachment in raven DB.
Now, trying to display it on image control in asp.net page.

the attachment key is product/1

is the above code correct?
if so, whats the return type?

Ayende Rahien

unread,
Apr 5, 2011, 8:22:41 AM4/5/11
to rav...@googlegroups.com, Karuna V
It returns a byte array.
Yes, the code is correct.

Karuna V

unread,
Apr 5, 2011, 9:40:05 AM4/5/11
to ravendb
It worked well.
I could save and retrieve images.

Now, how can I relate these images to already created products?
I have created a property called imageKey in Product.cs.
How can i update the existing products with this imageKey?

Ayende Rahien

unread,
Apr 5, 2011, 10:22:36 AM4/5/11
to rav...@googlegroups.com, Karuna V
var p  = session.Load<Product>("products/1");
p.ImageKey = "products/1.img";
session.SaveChanges();

Karuna V

unread,
Apr 6, 2011, 5:12:48 AM4/6/11
to ravendb
Is there a way to talk directly to lucene from ravendb?

Ayende Rahien

unread,
Apr 6, 2011, 5:15:16 AM4/6/11
to rav...@googlegroups.com, Karuna V
Via plugins, yes.

Matt Warren

unread,
Apr 6, 2011, 5:22:16 AM4/6/11
to ravendb
There's quite a few parts of Lucene that Raven already exposes. For
instance you can set the field indexing and storage mode and control
the analyser used. See http://ravendb.net/documentation/how-indexes-work
and http://ravendb.net/documentation/docs-http-indexes-querying

Dody Gunawinata

unread,
Apr 7, 2011, 4:29:34 AM4/7/11
to rav...@googlegroups.com, Matt Warren
Is it actually a good idea to a potentially large binary file as
attachment in RavenDB? I remember reading somewhere that the
functionality was geared towards storing js files to support "ravendb
apps" scenario.

--
nomadlife.org

Ayende Rahien

unread,
Apr 7, 2011, 4:32:02 AM4/7/11
to rav...@googlegroups.com, Dody Gunawinata, Matt Warren
We are working on making attachments much better in terms of size, but even so, we consider attachments to be fairly small < 1 MB in size for the most part.
There is another product, RavenFS, which we consider, for very large files, but that is unlikely to show up before the end of the year.

Matt Warren

unread,
Apr 7, 2011, 5:09:30 AM4/7/11
to ravendb
RavenFS sounds cool and intriguing, is this something based on Munin
or something completely new?

Also what could it be used for, what problem is it trying to solve?

On Apr 7, 9:32 am, Ayende Rahien <aye...@ayende.com> wrote:
> We are working on making attachments much better in terms of size, but even
> so, we consider attachments to be fairly small < 1 MB in size for the most
> part.
> There is another product, RavenFS, which we consider, for very large files,
> but that is unlikely to show up before the end of the year.
>
> On Thu, Apr 7, 2011 at 11:29 AM, Dody Gunawinata <empirebuil...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Is it actually a good idea to a potentially large binary file as
> > attachment in RavenDB? I remember reading somewhere that the
> > functionality was geared towards storing js files to support "ravendb
> > apps" scenario.
>
> > On Wed, Apr 6, 2011 at 11:22 AM, Matt Warren <mattd...@gmail.com> wrote:
> > > There's quite a few parts of Lucene that Raven already exposes. For
> > > instance you can set the field indexing and storage mode and control
> > > the analyser used. Seehttp://ravendb.net/documentation/how-indexes-work
> > > andhttp://ravendb.net/documentation/docs-http-indexes-querying
> ...
>
> read more »

Ayende Rahien

unread,
Apr 7, 2011, 5:12:17 AM4/7/11
to rav...@googlegroups.com, Matt Warren
Geo-distributed sharing of very large files ( 100s of MB or multiple GBs).
The basic idea is that you set multiple nodes and they can replicate those files between them. 
It is a pretty specific thing.

Matt Warren

unread,
Apr 7, 2011, 7:18:32 AM4/7/11
to ravendb
Thanks for the info, it sounds cool.

On Apr 7, 10:12 am, Ayende Rahien <aye...@ayende.com> wrote:
> Geo-distributed sharing of very large files ( 100s of MB or multiple GBs).
> The basic idea is that you set multiple nodes and they can replicate those
> files between them.
> It is a pretty specific thing.
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages