Skip to content
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

Shema Design Assignment: question regarding Part 2: Creating an Embedded Relationship #7

Open
Wonderband opened this issue Jul 22, 2016 · 2 comments

Comments

@Wonderband
Copy link

Wonderband commented Jul 22, 2016

So, we create a new Racer and insert the Race as the first element within an array within Racer called races

races.find(:name=>{:$exists=>true}).each do |r|
result=racers.update_one({:name=>r[:name]},
{:name=>r[:name],
:races=>[
{:_id=>r[:_id],
:number=>r[:number],
:group=>r[:group],
:time=>r[:time]}

]}, {:upsert=>true})

It seems to me, that this way we'll override the data each time, when the same racer took part in the different race. So, our array field races, embedded in racer always will consist of one element.
I think it should be another way: one racer can take part in many races, so we shouldn't override the races[] data, but do PUSH data to array or something like that.
Am I right?

@ejavaguy
Copy link
Contributor

You are correct in your assertions about the over-write. The solution was
limited to the test data -- which was formed from a single race.

The goal was to re-share the collections to allow for multiple races,
demonstrate migration of the database to a new (virtual) schema, and the
use of a few MongoDB and Ruby commands along the way. To make your
correction -- we could need to adjust our initial query to group all
results by racer, leverage the aggregation framework to form the re-shaped
document , and use $addToSet for the races as you suggest. If you make that
augmentation, we can add that as a footnote to the assignment step.

Nice observation. If you would like to discuss this more, please create a
newsgroup POST in the week#2 forum since that is focused at MongoDB and
this location reaches a much broader audience.

jim

On Fri, Jul 22, 2016 at 12:08 PM, Koniushenko [email protected]
wrote:

So, we create a new Racer and insert the Race as the first element
within an array
within Racer called races

races.find(:name=>{:$exists=>true}).each do |r|
result=racers.update_one({:name=>r[:name]},
{:name=>r[:name],

_:races=>[ {:_id=>r[:id], :number=>r[:number], :group=>r[:group],
:time=>r[:time]}

]}, {:upsert=>true})

It seems to me, that this way we'll override the data each time, when the
same racer took part in the different race. So, our array field races,
embedded in racer always will consist of one element.
I think it should be another way: one racer can take part in many races,
so we shouldn't override the races[] data, but do PUSH to array or
something.
Am I right?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#7,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AHCb2qbKEJpJv1zj3SwnUuYJUmlQAUAVks5qYOr4gaJpZM4JS53y
.

@Wonderband
Copy link
Author

OK, thank you for your answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants