AngularFire2 > Firestore > Complex queries with `in` or not `in` - node.js

I've been following this tutorial to build a rating system.
Which suggests the following collections:
Movies:
uid
title
Users
uid
name
Stars
movieId
userId
value
Now, suppose I wanted to display only pending movies to be rated by users.
How could this query be written?

If you think you'll actually have a small number of pending and not an imdb sized list you could
Get all movies
Get all movies rated by this user
Use the two collections to firm a list of movies they need to review
Or
1. Add a sub collection to user called unrated movies
1. every time a niche is added to the db add it to every user's collection
1. When a user reviews a movie delete from collection
Or have a value in the stars db of - 1 to represent unrated, add that for all users and all movies, then query by user ID and value is - 1

Related

Search collection fields and populated fields at the same time

I have 2 collections as the following:
Products
- name (String)
- price (number)
- category (ObjectID refs to Category collection)
Category
- name
And we have search query for example "some query"
I'm trying to get the records where product.name or product.category.name partially includes the search query
If you are using Mongoose populate, that uses DBRef, which is not easily used on the server side.
If the Products document contains only the _id of the corresponding Category document, you would need to use aggregation to read all Products documents, and for each one load all of the corresponding Category documents, then filter using a regular expression.
That is not scalable.
If you denormalize a little bit by having the Products document contain both the _id and name of the Category (assuming Category actually has other data), you could use a text index on the Products collection, or if you are using Atlas, full text search in the Products collection.

issue with the usage of BELONGSTOMANY or HASMANY on sequelize

I've been using NODE.JS - SEQUELIZE to deal with POSTGRES database. But, it's been a while that I am facing an issue.
I have two TABLES:
FIRST TABLE: Purchases. Inside of this table, there is a column which keeps the foreign key of the Products table, because they are associated. But, as long as I'veen been coding, I realized that I needed to "insert" more than one products at once, like an array, for those people who will buy more than one product at once.
SECOND TABLE: Products.
I want something like this => Allow to a purchase inside of Purchases to have more than one products associated with. But all that I can do is make the product foreign key column in purchases table accepts only intenger (ID) of only one product.
For exemple:
The user X buyed multiple products, so then in product in Purchases will have the products [1,3,5] and these numbers are the product's ID that I would like to associate with the Products table.
print of the PURCHASES MODEL: purchases MODEL(not the migration) on sequelize
print of the PURCHASES TABLE: purchases table structure
print of the PRODUCTS TABLE: products table structure
The conclusion I've have reached was using "Belongs to MANY" or "Has many", but I don't how.
Thanks.
I propose you to add another table to achieve multiple products in one order:
ORDER table - stores one record per a customer order (all columns that related with an order as a whole)
ORDER_ITEMS - stores items inside each order (columns: a link to ORDER, a link to PRODUCT, a quantity, a price and other related columns (a discount and so on)
PRODUCT - stores a catalog of products to buy

handling concurrency for last item in inventory

We've 1 book left in the inventory. and two people are trying to get the same book ( say person x and person y ). Person x has added book to the cart and about to make payment and person y has also added book to the cart. How would you solve this concurrency problem ?
Based on your description, Looks like you are allowing users to add last item to cart that mean there is no hold on item while its in cart, Now you can add check during check out, checking for item availability like database constraint that stock can not be less than 0 in this case your database transaction will fail and would return error. You can reply back with message saying item out of stock.

Retrieving just the joined entities from Core Data

I have a movie showtimes entity which has a one-to-one relationship to a movie entity. The inverse (movie -> movie showtime) relationship is a one-to-many relationship. If a movie is deleted, the associated movie showtimes will also be deleted, but if a movie showtime is deleted the associated movie will stay. (Not sure how much of that is relevant but wanted to clarify the situation as much as I could)
Now, is there a way to query Core Data to get only the unique movies for which I have showtimes?
Is it possible to select from the movie showtimes and somehow restrict the results to just the associated unique movies? Or would selecting from the movie entity bring back only the movies with a matching row in the movie showtime entity?
Sure. Write a fetch request on Movie with no restrictions, and you'll get all Movie instances.
Taking your questions in turn:
Now, is there a way to query Core Data to get only the unique movies for which I have showtimes?
You could use a predicate to select movies where the count of showtimes is greater than zero:
"showtimes.#count > 0"
Is it possible to select from the movie showtimes and somehow restrict the results to just the associated unique movies?
If you have (courtesy of your first query) an array (say scheduledMovies) of movies which have showtimes, then you can fetch the associated showtimes using a predicate like this:
"movie IN %#", scheduledMovies
Or would selecting from the movie entity bring back only the movies with a matching row in the movie showtime entity?
If you fetch movies, then you will get ALL movies, unless you specify a predicate as per your first question. But if you fetch showtimes, you can get an array of the associated movies using key value coding with the key:
"#distinctUnionOfObjects.movie"
The resulting array will not have any movies with no showtimes. (The first part of this key removes any duplicates, since several showtimes might have the same movie).

Backbone collection and model structure

I have an app in backbone that has essentially 3 main components: groups, users, and posts. I have models and collections for all 3, and then on top of that I am tracking in depth analytics for each type. Multiple users belong to a single group by the way.
For example, I have another collection called groups_index that has the fields: date, average # of posts, and average post length ( with a new row for each date ). And then I also have a user_index collection that has the fields: date, group_id, average # of posts, and average post length.
I want to be able to generate charts that show the average number of posts for a group across time and the same for the users of a specific group.
Does it make more sense to combine everything into the groups_index collection and add an user_id field? Or would that over-complicate it when showing the group average charts?

Resources