New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection methods should support Regexp objects in selectors #346

Closed
jbear opened this Issue Sep 20, 2012 · 6 comments

Comments

Projects
None yet
2 participants
@jbear

jbear commented Sep 20, 2012

With the Leaderboard example, doing the following from browser console doesn't seem work:

Players.update({name: /Ada/},{$set: {score: 29});

While the same code works in Mongo shell.

@glasser

This comment has been minimized.

Member

glasser commented Sep 20, 2012

Interesting. I do agree that I see this, although find({name: /Ada/}) does work.
(Note: your example is missing a }).

@glasser

This comment has been minimized.

Member

glasser commented Sep 20, 2012

OK, minimongo supports this properly, but when we send the update over the network, we serialize the regex as "{}".

You can work around this by now by doing

{name: {$regex: "Ada"}}

instead, but this is definitely a bug.

(Note: in this particular example,

{name: {$regex: /Ada/}}

may appear to work, but actually it will just match the first item in the collection, which in this case happens to be the Countess. If you switch to

{name: {$regex: /Grace/}}

you will see that it still matches the Countess rather than the Rear Admiral.

@glasser

This comment has been minimized.

Member

glasser commented Sep 20, 2012

There are two possible approaches to this one:

(a) A mongo-livedata specific approach (presumably in collection.js) which rewrites {keypath: RegExp} selectors into {keypath: {$regex: String}} selectors.

(b) A generic approach which changes our serialization strategy in livedata to use something a little smarter than JSON.stringify which preserves RegExp objects. (There's some similarity between this issue and #61, although RegExp is a built-in JavaScript type and the ObjectId is Mongo-specific.)

@jbear

This comment has been minimized.

jbear commented Sep 20, 2012

Thank you David, for the very helpful comments. And a BIG thank you for the fantastic work on Meteor! I love it....

I actually prefer $regex syntax, and probably wouldn't consider it as a bug.

Though I did find that update on multiple docs only appears to work on the first one:

Players.update({name: {$regex: "e$"}},{$set: {score: 101}}, true, true); // 2 players end in e but only one gets updated

Players.update({score: {$lt: 1000}},{$set: {score: 0}}, true, true); // Should apply to all players but only one works
@glasser

This comment has been minimized.

Member

glasser commented Sep 21, 2012

I'm not sure what the "true" and "true" you are passing are supposed to do. But you need to pass a multi option to modify more than one document; see http://docs.meteor.com/#update :

Players.update({name: {$regex: "e$"}}, {$set: {score: 101}}, {multi: true});
@jbear

This comment has been minimized.

jbear commented Sep 21, 2012

David, sorry to have wasted your precious dev time. I was reading the wrong manual -- mongodb (http://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29)!

I'm happy it's all working now update with $regex and multi. Thank you.

On 21/09/2012, at 10:34 AM, David Glasser wrote:

I'm not sure what the "true" and "true" you are passing are supposed to do. But you need to pass a multi option to modify more than one document; see http://docs.meteor.com/#update :

Players.update({name: {$regex: "e$"}}, {$set: {score: 101}}, {multi: true});


Reply to this email directly or view it on GitHub:
#346 (comment)

@glasser glasser closed this in 28a136d Dec 10, 2012

glasser added a commit that referenced this issue Dec 14, 2012

glasser added a commit that referenced this issue Dec 17, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment