Does document exist?

1308 views
Skip to first unread message

slav

unread,
Jul 29, 2010, 3:30:07 PM7/29/10
to ravendb
This is probably a simple thing, but I can't figure it out.

Is there an easy way to check that a document exists by document ID
without actually loading it? Create an index by id and query it?

On a side note, how would I create an index by document id? I need to
index by metadata, right? I've seen discussion, but not concrete
example.

Thanks.

Ayende Rahien

unread,
Jul 29, 2010, 4:06:10 PM7/29/10
to rav...@googlegroups.com
Huh?
Why not just session.Load<...>("doc/id") != null

Daniel Cazzulino

unread,
Jul 29, 2010, 4:07:54 PM7/29/10
to rav...@googlegroups.com
> without actually loading it

;)

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471

Ayende Rahien

unread,
Jul 29, 2010, 4:10:26 PM7/29/10
to rav...@googlegroups.com
What is _the point_.

Timothy Walters

unread,
Jul 29, 2010, 7:43:00 PM7/29/10
to ravendb
I imagine the cost of getting a very large document that we might just
want to apply a PATCH to would be one possible scenario. In this case
we simply want to confirm that the document exists before trying to
patch in a small addition/change.

An example might be a site that has large documents with attachments,
and allows comments. Lets also assume that documents are regularly
moved off to an Archive (maybe a different DB, or different document
type). If someone tries to add a comment, I need to check that it
still exists as Active, and if so patch it there, otherwise patch it
in the Archive. Not the best example, but it demonstrates some need.
Sure you could argue that it's not the best design, or could be worked
around in different ways, but it'd be nice if the system supported an
EXISTS operation, or something.

On Jul 30, 6:10 am, Ayende Rahien <aye...@ayende.com> wrote:
> What is _the point_.
>
> On Thu, Jul 29, 2010 at 11:07 PM, Daniel Cazzulino <dan...@cazzulino.com>wrote:
>
> > > without actually loading it
>
> > ;)
>
> > /kzu
>
> > --
> > Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> > 425.329.3471
>
> > On Thu, Jul 29, 2010 at 17:06, Ayende Rahien <aye...@ayende.com> wrote:
>
> >> Huh?
> >> Why not just session.Load<...>("doc/id") != null
>

Ayende Rahien

unread,
Jul 29, 2010, 9:04:11 PM7/29/10
to rav...@googlegroups.com
HEAD /docs/my_doc_key

Daniel Cazzulino

unread,
Jul 29, 2010, 9:42:09 PM7/29/10
to rav...@googlegroups.com

Would be nice to have a client api to do the same

from android nexus1

slav

unread,
Jul 29, 2010, 9:52:35 PM7/29/10
to ravendb
In my specific case orders come into my system from 3rd party. Each
order must have unique ID. If I save all orders before forwarding them
to be handled somewhere else.
In theory order with the same ID should come in only once. In practice
if it comes in again I need to notify the 3rd party so that they could
look into it.
So, each time an order comes in, I'll try to load it to see if it
exists. Most of the time I'll get null and things will go normal.

It doesn't really matter in this case if there's no way to check for
the doc existence by ID. Most of the time I won't be loading the
document anyway so no overhead.
I was just curious, since I'm still learning the system.

By the way, "HEAD /docs/my_doc_key" ? Is HEAD an HTTP operation? I
don't see it on the list (http://ravendb.net/documentation/docs-http-
api-index)

On Jul 29, 4:10 pm, Ayende Rahien <aye...@ayende.com> wrote:
> What is _the point_.
>
> On Thu, Jul 29, 2010 at 11:07 PM, Daniel Cazzulino <dan...@cazzulino.com>wrote:
>
> > > without actually loading it
>
> > ;)
>
> > /kzu
>
> > --
> > Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> > 425.329.3471
>
> > On Thu, Jul 29, 2010 at 17:06, Ayende Rahien <aye...@ayende.com> wrote:
>
> >> Huh?
> >> Why not just session.Load<...>("doc/id") != null
>

Ayende Rahien

unread,
Jul 30, 2010, 3:57:07 AM7/30/10
to rav...@googlegroups.com
HEAD is the equivalent of GET, it does the same thing, but return only the headers.

Damian Hickey

unread,
Dec 25, 2011, 5:01:51 AM12/25/11
to rav...@googlegroups.com
(Sorry for bumping an old thread but couldn't find a more recent discussion)

Is this funtionality exposed via the Client API?

Oren Eini (Ayende Rahien)

unread,
Dec 25, 2011, 5:09:35 AM12/25/11
to rav...@googlegroups.com
No, it isn't.
You can just call DatabaseDocuments.Get and see if you have null or not, instead.
Or send us a pull request.

Damian Hickey

unread,
Dec 25, 2011, 6:45:45 AM12/25/11
to rav...@googlegroups.com
Cheers for the quick response.


> Or send us a pull request

I will examine the API\Code and come back with a proposal.

Damian Hickey

unread,
Dec 27, 2011, 5:16:29 AM12/27/11
to rav...@googlegroups.com
> You can just call DatabaseDocuments.Get

Did you mean IDatabaseCommands.Get()?

If so, I propose adding:

bool IDatabaseCommands.Exists()

If you think this is reasonable, I can proceed with attempt at implementing.

(I think I replied to you directly by accident)

Itamar Syn-Hershko

unread,
Dec 27, 2011, 5:17:43 AM12/27/11
to rav...@googlegroups.com
store.DatabaseCommands.Get

It can all be done using a simple extension method...

Damian Hickey

unread,
Dec 27, 2011, 5:20:10 AM12/27/11
to rav...@googlegroups.com
Without loading the document?

Itamar Syn-Hershko

unread,
Dec 27, 2011, 5:25:57 AM12/27/11
to rav...@googlegroups.com
No, it will load the doc

You want to implement a Head() method then, which will return the etag or null if it doesn't exist. Better than Exists() IMO

On Tue, Dec 27, 2011 at 12:20 PM, Damian Hickey <dhi...@gmail.com> wrote:
Without loading the document?

Damian Hickey

unread,
Dec 27, 2011, 5:32:31 AM12/27/11
to rav...@googlegroups.com
Ok will do it that way. Can do an 'Exists' extension method over that.

Damian Hickey

unread,
Dec 27, 2011, 5:36:14 AM12/27/11
to rav...@googlegroups.com
Guid? IDatabaseCommands.Head(string key)

Oren Eini (Ayende Rahien)

unread,
Dec 27, 2011, 5:41:05 AM12/27/11
to rav...@googlegroups.com
No, we will also send the metadata.

JsonDocumentMetadata IDatabaseCommands.Head(string key);

On Tue, Dec 27, 2011 at 12:36 PM, Damian Hickey <dhi...@gmail.com> wrote:
Guid? IDatabaseCommands.Head(string key)

Damian Hickey

unread,
Dec 27, 2011, 5:47:34 AM12/27/11
to rav...@googlegroups.com
Ack

Damian Hickey

unread,
Dec 27, 2011, 8:50:52 AM12/27/11
to rav...@googlegroups.com
I have it implemented and tested except for one problem..  I wasn't getting a Last-Modified value when using DocumentStoreServer.

In Raven.Database.Server.Responders.Document.Head(), last line:

    context.WriteHeaders(documentMetadata.Metadata, documentMetadata.Etag.Value);

...the documentMetadata.LastModified is ignored / omitted. Was this intentional? This means the metadata when using DocumentStoreEmbedded had a LastModied value, but DocumentStoreServer did not.

As you may see from my pull request ( https://github.com/ravendb/ravendb/pull/416/files#L4R127 ) , I've added a hack to fix this (if it wasn't intentional). Let me know if there is a better way.

Cheers

- Damian


Oren Eini (Ayende Rahien)

unread,
Dec 27, 2011, 9:03:48 AM12/27/11
to rav...@googlegroups.com
I'll look at that tomorrow, in the mean time, I need your CLA, see here:  http://ravendb.net/faq/contributing 

Damian Hickey

unread,
Dec 27, 2011, 11:22:40 AM12/27/11
to rav...@googlegroups.com

I've already signed and submitted a CLA for a previous contribution. Do I need to do so again?

Oren Eini (Ayende Rahien)

unread,
Dec 28, 2011, 6:54:22 AM12/28/11
to rav...@googlegroups.com
No, I didn't check that, thanks, I'll look at that shortly.

Oren Eini (Ayende Rahien)

unread,
Jan 1, 2012, 6:40:29 AM1/1/12
to rav...@googlegroups.com
Pulled

David Zurillo

unread,
Mar 26, 2013, 12:24:59 AM3/26/13
to rav...@googlegroups.com
I know this is over a year ago(apologies). Is the proposed method implemented, because I cannot seem to find it.

This is going to be very useful to use it for uniqueness constraint checking for write validation to a client when documents use a natural key for document ids.

E.g UsedUserEmailAddress has id set to email addrress and a single property to the non-natural key. If the mentioned method was implemented then one would no-longer need this document(which only exists so the load by id null check can happen without returning the entire user document)

Kijana Woodard

unread,
Mar 26, 2013, 12:48:51 AM3/26/13
to rav...@googlegroups.com
The original thread didn't come through for me, but maybe you can use the "Head" document command. 



--
You received this message because you are subscribed to the Google Groups "ravendb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



David Zurillo

unread,
Mar 26, 2013, 3:57:15 AM3/26/13
to rav...@googlegroups.com
I thought DatabaseCommands cannot be used with sharded document store? If so then this will not work if you plan to implement sharding.

Kijana Woodard

unread,
Mar 26, 2013, 10:01:51 AM3/26/13
to rav...@googlegroups.com

Sharding? As I said, the original thread doesn't show. Link?

On Mar 26, 2013 2:57 AM, "David Zurillo" <david....@gmail.com> wrote:
I thought DatabaseCommands cannot be used with sharded document store? If so then this will not work if you plan to implement sharding.

Kijana Woodard

unread,
Mar 26, 2013, 5:03:32 PM3/26/13
to rav...@googlegroups.com
Ok. I can see the whole thread on the ng client. The full thread didn't come in gmail.

You are right about the sharded document store:

But can you use DatabaseCommandsFor:


On Tuesday, March 26, 2013 9:01:51 AM UTC-5, Kijana Woodard wrote:

Sharding? As I said, the original thread doesn't show. Link?

On Mar 26, 2013 2:57 AM, "David Zurillo" <david....@gmail.com> wrote:
I thought DatabaseCommands cannot be used with sharded document store? If so then this will not work if you plan to implement sharding.

--
You received this message because you are subscribed to the Google Groups "ravendb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages