M220N Migration

In your ticket for this lab, you state that I will need to take the results from DatePipelineResults and pass it to the BulkWriteAsync method.

This in itself is quite simple since you have created the Pipeline for the student. I would however like to get clarification on one thing. It states in the program that I need to use a ReplaceOneModel…

Where is there any information on the ReplaceOneModel?? It would seem that to pass the results of the pipeline into the bulk write one would only need to insert the results inside the ()…

 bulkWriteDatesResult = await _moviesCollection.BulkWriteAsync(datePipelineResults);

But of course this is not the only thing that needs to be done… My question is How is the ReplaceOneModel used and where is this in the video??

Hi @David_Thompson.

You can learn more about the ReplaceOneModel in the MongoDB docs for the C# driver. I hope it helps.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

This is really… really frustrating…

I understand that I need to use ReplaceOneModel somehow, but the documentation provided gives me no hint as to how to use it… I need to somehow use it with the datePipelineResults to get the BulkWriteAsync to work… (At least that is my understanding)

I get the prior lessions… I don’t get this lesson and I am really REALLY frustrated… I can watch the video 20 times and it will have nothing ABSOLUTLY nothing about what is expected in the lab… So… I really don’t know where you want me to go.

If it is similar to the write concern lab in usage, then the ReplaceOneModel needs to go in the line with

await _moviesColleciton.BulkWriteAsync() line… But where and how… I am not sure.

1 Like

Is this how I use it? I am asking so I can do the other function call.

 bulkWriteDatesResult = await _moviesCollection.BulkWriteAsync((IEnumerable<WriteModel<Movie>>)datePipelineResults);

I ran the program and I am getting a bad authentication message. Is this connection string right?

mongodb+srv://M220student:m220password@mflix.fn5qt.mongodb.net/sample_mflix?retryWrites=true&w=majority"

Got the connection to work… But I get the following error

System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast object of type 'System.Collections.Generic.List`1[M220N.Models.Movie]' to type 'System.Collections.Generic.IEnumerable`1[MongoDB.Driver.WriteModel`1[M220N.Models.Movie]]'.
  Source=Migrator
  StackTrace:
   at Migrator.Program.<Main>d__2.MoveNext() in D:\65945\M220N\Migrator\Program.cs:line 38

So… Obviously I am using the ReplaceOneModel wrong. If I am having trouble understanding the Driver Documentation sent earlier… Where can I go to learn how to use the ReplaceOneModel in the context needed?

BulkWrite

Is this the way I need to be looking at the problem? I switched the tab to update many

Is this the ReplaceOneModel they are talking about?

In the other instances of the M220 course the setup instructions ask to

create the application MongoDB database user required for this course:

        username: m220student
        password: m220password

This means

no.

You have a typing error because it should be m220student rather than *M220student. From your edit history I can see that you have noticed and corrected the same typing error for the password.

Thanks Steevej…
I noticed the typo also after posting… I actually had another error in that I never stipulated the sample_mflix database.

I am having other troubles though… I am struggling with how to convert the dataPipelineResults. To pass it into the BulkWriteAsync function I need to use the ReplaceOneModel function…

All my research tells me it changes the documents that I am inserting into the bulk write. But BulkWrite is I believe IEnumerable wheras the variable is a Generic. List.

In the above posts I referenced BulkWrite but I am unsure if this is the right reference.

I have only worked on the first bulk update because I want to get this one right before working on the last one.

Any ideas or recommendations would be appreciated.

Hi @David_Thompson, take a look at the following:

var bulkWriteDatesResult = await _moviesCollection.BulkWriteAsync(
  datePipelineResults.Select(updatedMovie => new ReplaceOneModel<Movie>(
    new FilterDefinitionBuilder<Movie>().Where(m => m.Id == updatedMovie.Id),
    updatedMovie)))

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Thank you so much for your help… I now see how to use the ReplaceOneModel… I do have some questions I need to ask though. Please bear with me since I am relatively new to Visual Studio

Where I get confused in Visual Studio is when I have a function that returns a group… Lets say 50 records…

In conventional programming (JavaScript is the language I went to college for) you would use a For loop to iterate through the results.

Foreach (record in records) … then code…

Also similar to for(i =0, i < datePipelineResults.length, i++) would also iterate through the return.

That being said… It looks to me like I am passing in the datePipelineResults using the .Select operator (I didn’t know that iterated through the results) I am taking the variable updatedMovie and creating a new document with the ReplaceOneModel, filtering the documents on the ID field and updating the collection.

Is this right?

I need one more clarification… Can I accomplish the same result if I were to filter based on the field that was changing? Lastupdated for date pipeline and imdb.rating for the rating pipeline?

Hope I am not being a thorn in your side…

For future updates in this course… For beginners, it may be of value to explain IEnumerable, List, and Task so that the student can understand how to transfer data between them. If not including this in the course… at least links for the student to go and read about it from the Microsoft Documentation pages. (I know that we can find them outside of the class) LOL

Hi @David_Thompson,

This is correct.

I am not sure if I am getting this correctly, can you share an example, with appropriate input, output, and the required behavior for the same?

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Sure… Instead of

 datePipelineResults.Select(updatedMovie => new ReplaceOneModel<Movie>(                         new FilterDefinitionBuilder<Movie>().Where(m => m.Id == updatedMovie.Id), updatedMovie))                  );

Can I use this query instead and get the same results… I edited it and it runs, but I have already altered the data so I get 0 documents changed.

  datePipelineResults.Select(updatedMovie => new ReplaceOneModel<Movie>(                         new FilterDefinitionBuilder<Movie>().Where(m => m.LastUpdated == updatedMovie.LastUpdated), updatedMovie))                 );

Would this also produce the same results?

I don’t think it will. The migration process is to convert the date from string to date. So m.LastUpdated is probably a string and updatedMovie.LastUpdated is surely a date. They are not type compatible so the equality m.LastUpdated == updatedMovie.LastUpdated will most likely select no documents. The condition m.Id == updatedMovie.Id is a safer one in this context.

But let assume m.LastUpdated == updatedMovie.LastUpdated returns true even when they differ in types. You are using replaceOne which will replace the first document that matches. Which document do you think will be updated, at your second or third replaceOne of the list, if 2 or more documents have the same LastUpdated field?

1 Like

If I am understanding you correctly… the reason I use .Id is because it will ALWAYS be a unique identifier… Am I on the right track?

Yes that is the reason.