You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The only way to create an object or to add a pair (key, value) in an object is to use
r.expr{{ key: "value" })
or
r.expr({}).merge({key: "value" })
In every case, we can not use a dynamic key, something like
r.expr({ r.expr("key"): "value" })
For the JavaScript driver, the problem lies in JavaScript. { r.expr("key"): "value"} is going to throw an error, and doing
var obj = {}
obj[r.expr("key")] = "value"
will create this object
{
"[object Object]": "value"
}
We will need to add a new method in ReQL that takes pairs of (key, value) and creates an object. Something like that
r.object(["key1", "value1"], ["key2", "value2"] )
The use case is the following one.
I have a table "posts", "tags", "links_posts"tags" and I want to retrieve all the posts with an attribute tags that is an object that look likes that
tags: {
tag1: true,
tag2: true,
tag3: true
}
So I can display all the post and let the users quickly filter them by tags (without having to fire a new request/query) or to go through an array of tags. I would just have to check if post.tags[tag_selected_by_user] is defined.
I think it's something pretty useful for web developers.
Some time ago we made a conscious decision not to put effort into supporting these types of use cases (dynamic attribute shenanigans) because it complicates the language, has dubious benefits, and potentially significantly complicates performance analysis later on. In this case, instead of an object tags: { tag1: true, tag2: true, ...}, it would be much much better to structure it like this: tags: ['tag1', 'tag2', ...]. You can then query arrays using array operations (which we're working on).
Closing, due to lack of compellingness.
Shakespearean tl;dr:
Compelling use case here I do not see,
I therefore this bug will promptly close,
Dynamic attributes in general are,
Abusive and uncalled for I should think.
@coffeemug in the use case I mentioned, I want to filter on the client side (instead of firing a second query, so it has nothing to do with the array operations).
I want to filter on the client to keep the app fast and responsive.
@mlucy's solution works for me, even if it's not really nice (not sure if the proposedr.obj() is better though).
Reopening because I think @coffeemug didn't really understand the use case, and because I would find really irritating not to be able to do it (more than not being able to do operations on arrays/string)
The thing is I want the users to be able to use whatever name for fieldName (so it's a variable). The only way now to do it is to coerce from array to object.
The original proposal to fix this issue was r.object(["key1", "value1"], ["key2", "value2"] ). Is this much better than r.expr(["key1", "value1"], ["key2", "value2"]).coerceTo('object')? Considering that the syntaxes are nearly identical, I don't think this is very important right now (unless there is a better proposal for this).
Activity
coffeemug commentedon Feb 1, 2013
Some time ago we made a conscious decision not to put effort into supporting these types of use cases (dynamic attribute shenanigans) because it complicates the language, has dubious benefits, and potentially significantly complicates performance analysis later on. In this case, instead of an object
tags: { tag1: true, tag2: true, ...}, it would be much much better to structure it like this:tags: ['tag1', 'tag2', ...]. You can then query arrays using array operations (which we're working on).Closing, due to lack of compellingness.
Shakespearean tl;dr:
Compelling use case here I do not see,
I therefore this bug will promptly close,
Dynamic attributes in general are,
Abusive and uncalled for I should think.
mlucy commentedon Feb 1, 2013
Version 1.4 supports coercions between arrays and objects. So the following should work:
neumino commentedon Feb 1, 2013
@coffeemug in the use case I mentioned, I want to filter on the client side (instead of firing a second query, so it has nothing to do with the array operations).
I want to filter on the client to keep the app fast and responsive.
@mlucy's solution works for me, even if it's not really nice (not sure if the proposed
r.obj()is better though).Reopening because I think @coffeemug didn't really understand the use case, and because I would find really irritating not to be able to do it (more than not being able to do operations on arrays/string)
coffeemug commentedon Feb 1, 2013
Oh, sorry, I see what you mean. I'd be cool with putting sugar on
coercefor this. Moving to post-protobuf.coffeemug commentedon Feb 14, 2013
BTW, it would be nice to have sugar over
reduce(merge). Basically a variadicr.mergethat also accepts arrays of objects.neumino commentedon Jul 10, 2013
I just bumped into this.
Use case: Transform the results of a join.
I want to do a join, and do something like
The thing is I want the users to be able to use whatever name for fieldName (so it's a variable). The only way now to do it is to coerce from array to object.
While it works, it makes things quite ugly.
coffeemug commentedon Jul 10, 2013
The original proposal to fix this issue was
r.object(["key1", "value1"], ["key2", "value2"] ). Is this much better thanr.expr(["key1", "value1"], ["key2", "value2"]).coerceTo('object')? Considering that the syntaxes are nearly identical, I don't think this is very important right now (unless there is a better proposal for this).mlucy commentedon Jul 10, 2013
We could always do
r.obj('key1', 'val1', 'key2', 'val2').neumino commentedon Jul 10, 2013
I would be cool with
mlucy commentedon Jul 10, 2013
sethas destructive connotations to me. How aboutadd_fieldorwith_field?neumino commentedon Jul 10, 2013
Yep,
add_fieldis fine toowith_fieldis a little too close tohas_fieldfrom my point of view.coffeemug commentedon Jul 10, 2013
Ok, I'll move this to optional so it doesn't get lost in the woods.
coffeemug commentedon Jan 14, 2014
Closing in favor of #1856.