From the course: Vector Databases in Practice: Deep Dive

Solution: Add another object collection

From the course: Vector Databases in Practice: Deep Dive

Solution: Add another object collection

(upbeat music) - [Instructor] Okay, let's now have a look at my challenge solution. We'll need to create our collection first, which I'll do with this client.collections.create method. We'll give you the name Synopsis and use the same modules as above. We'll need to create a property called body and it'll have a text data_type. So these parts are essentially identical to the review collection above. Something the review collection doesn't have is references. For Synopsis, here's one of them here. We'll call it forMovie, since this is a synopsis for a particular movie and we'll point it to the Movie collection. And that's the Synopsis collection defined. The next task was to add a reference property to the Movie collection pointing to the Synopsis collection. And our hint was to use the movies.config.add_reference method. So let's start with that. And at this point you might not have been so sure what to do next, but if you move your mouse over this method, you'll see that the argument should be of the ReferenceProperty data type. So following that, we'll add an instance of the ReferenceProperty class with the name hasSynopsis. Remember, this is for pointing from the movie to the synopsis and target the Synopsis collection. And if we now run this code, it'll redefine our collections with the difference being that before we had two collections for Movies and Reviews. Now you have three with Movies, Reviews and Synopsis. With this done, let's now turn to the imports. The first thing to do, of course, is then to load or get the synopsis collection. And then the process of importing the synopsis objects would be very similar to what we showed before for the other collections. So we'll create a list of objects, which are the synopses in this case, and iterate over the data rows. Now our only property is called body, and it comes from the Synopsis column. As before, let's create IDs for our Synopsis objects. Now, we could use the synopsis text to generate these, but I'm actually just going to use the movie ID here. Hey, and you might ask, doesn't that overwrite the movie objects, which will have the same ID? Well, actually, in this case, the movie items will be safe. The reason being that these synopses will be in a separate collection from the movies, and each collection is a distinct namespace. So then we can build a data object just like before with the properties and the UUID specified. Remember to add the reference here under the ReferenceProperty name for movie with the target object being the movie collection, and they'll have the same movie ID since you're now pointing to the movie object with that same ID, but again, in a different collection. Don't forget to add the data object from each iteration to our list. And definitely don't forget to insert the list of objects into our database with the insert_many method. We've got some print statements here also to print the responses and make sure that there were no errors. And then once the synopsis objects are created, we need to create additional references that point from each movie back to each synopsis. This is done similarly to creating objects, but we'll build a list of references this time, not a list of objects. And the method that we use to insert these references will be different again, but let's go through the steps one by one. First we'll create a list of references and iterate through our data. We'll need UUIDs of the synopsis and the movie, but remember that we use the movie ID for both, and then we'll create the reference object that goes from the hasSynopsis property, but more specifically from the movie object to the synopsis object. Remember that they have the same UUID. Append the object and then add them all using the reference_add_many method. And you're done. Now running this code will add your data to the database, including the new references. Note that the Movie and synopsis collections now have what's called a two-way reference because these objects refer to each other in both directions, and that means they can be retrieved in either direction by retrieving a movie from a corresponding synopsis or vice versa, which can be very handy. If you've come this far, well done. You should now have a database that's identical to the demo database on the cloud, and that also means you can use either of the demo or your own database to build your web app in the next chapter.

Contents