ArangoDB equivalent of find_or_create_by?

73 views
Skip to first unread message

Mike Williamson

unread,
Jul 3, 2014, 3:27:03 PM7/3/14
to aran...@googlegroups.com
ActiveRecord has a very useful method call find_or_create_by. The description:

Finds the first record with the given attributes, or creates a record with the attributes if one is not found

This is pretty widely used and is very helpful. Is there an ArangoDB equivalent?

Right now it looks like my only option is:

arangosh [_system]> db.cities.firstExample({name: "Houston"})
null

and if that returns null, then I go ahead with the creation:

arangosh [_system]> db.cities.save({name: "Houston"})
{
  "error" : false,
  "_id" : "cities/517290785181",
  "_rev" : "517290785181",
  "_key" : "517290785181"
}

If I actually want the document (_id/_key and all the attributes) I either have to assemble it myself from the information I now have, or make a third roundtrip to the server to retrieve it in its entirety.

Any thoughts on how I might achieve something like find_or_create_by with Arango with the minimum (ideally one) roundtrips to the server?
I am using Arango 2.2 beta... is there new AQL stuff that might help?

Thanks

Mike

Frank Celler

unread,
Jul 8, 2014, 3:37:47 AM7/8/14
to aran...@googlegroups.com
Hello Mike,

we recently added INSERT and REPLACE to AQL (in 2.2). We will also add a REPLACE_OR_INSERT (as you described) in one of the next versions.

Stay tuned
  Frank

Mike Williamson

unread,
Jul 8, 2014, 9:47:08 AM7/8/14
to aran...@googlegroups.com
Awesome!

:)

Mike Williamson

unread,
Jan 16, 2015, 1:43:51 PM1/16/15
to aran...@googlegroups.com
I just found myself wishing this existed and remembered I had mentioned it before.
Did this end up making it onto the roadmap somewhere?
Should I open an issue on Github?

Thanks and keep up the great work!

Mike

Jan

unread,
Jan 19, 2015, 6:25:22 AM1/19/15
to aran...@googlegroups.com
Hi Mike,

no, it's currently not on the roadmap. Opening a feature request on Github is a good idea. If you could do that, that would be nice.

About the feature itself, my understanding is as follows:

The general form is:
db.collection.findOrCreate(searchAttributes, createAttributes);

The operation result is:
  - look for a document with searchAttributes in collection
    - if no such document exists, create it by merging the searchAttributes and the createAttributes, finally return the created document
    - if such document exists, return the first one. In case multiple such documents exist, return one of them (which one is undefined)

Is that correct?

Best regards
Jan

Mike Williamson

unread,
Jan 19, 2015, 10:48:52 AM1/19/15
to aran...@googlegroups.com
Thanks Jan,
I've opened issue 1216, reconsidering this a little in light of the new pseudo-values used with insert...
Let me know what you think.

Mike

Jan

unread,
Jan 20, 2015, 4:44:18 AM1/20/15
to aran...@googlegroups.com
Thanks. I have updated the Github issue with a suggestion.
Reply all
Reply to author
Forward
0 new messages