Is it appropriate that my collections have a key call session so that I can identify from whom this data belongs to? For example, I have few sets of data that store books. How to identify in nosql DB(MongoDB) that a set of data belongs to which user? I know in mysql we simply design the table using Foreign Key, but how can I do it in nosql?
What I can think of is I will have these data :
{
bookId:1,
bookName: "soemthing",
userId:1
}
{
another_collection_key:1,
another_value: "soemthing",
userId:1
}
where every set of data will have userId, correct?
The best way is to create a user collection and a book collection. In each book collection add a list of type 'user'. Sample collections given below -
User{id,name}
Book{id,name,list<user>}
This way each book can store all the users who have that book.
The other way is to create 3 collections - books, users and a link collection for linking book and user.
Sample collections given below -
User { id,name }
Books { id,name }
Lnk_User_Book { User_Id,Book_Id }.
Related
Good morning colleagues,
I want to make a query regarding how it would be formulated and what would be recommended and the most optimal for making queries with a large number of elements.
I have an api using express that creates a new mongodb model with a unique name and then includes elements inside.
Example:
Collections
*product234 => includes elements => { _:id: ...., name: ... }, { ...}
*product512 => includes elements => { _:id: ...., name: ... }, { ...}
Each collection hosts more than 5000 elements and I want to create a search engine that returns all the results of all the collections that match the "name" that I will send in the request.
How could I perform this query using mongoose? Would it be viable and would it not bring me performance problems by having more than 200 collections and more than 5000 elements in each one?
Answer (Edit):
As i see in the comments the best solution for this for making fast queries is to create a copy of the element with only name or needed values, reference id and reference collection name into a new collection named for example "ForSearchUse" and then make the query to that collection, if complete info is needed then you can query it to the specific collection using the reference id and name of the element
Whenever a user uses the Firebase Auth to register on my app, I create a document in a users collection of Firestore that stores metadata such as pseudo, userType, gender ...
To do that, the document id is exactly the same as the uid provided automatically by Firebase Auth (from the user UserRecord object)
Now, my app needs to fetch a user randomly from the users collection in Firestore.
I read Firestore: How to get random documents in a collection but this post suggest that I create the ID myself. The app is already built using the FirebaseAuth generated ID so what is the solution ?
A common practice in firestore is to create a "--Stats--" document within a collection (--Stats-- being the document id). This document can house information about the collection (number of documents, last updated, collection privileges etc.).
In your case, you could use cloud functions/triggers to keep tract of the total number of users in the users collection and add the id of a new user to a "userIds" array. You could keep both of these fields in the users collection's --Stats-- document. This way, when you wanted to get a random user, you could randomly generate a number betweeen 0 and the document count, then use it as an index of the userIds array. I might look something like this:
var usersCollectionRef= db.collection("users");
usersCollectionRef.doc("--Stats--").get().then((doc) => {
let numberOfUsers = doc.data().numberOfUsers;
let userIdArray = doc.data().userIds;
let randomNumber = Math.floor(Math.random() * (numberOfUsers + 1));
return usersCollectionRef.doc(userIdArray[randomNumber]).get();
}).then((doc) => {
...do something with the user's document
})
I have a REST API which serves from a database in MongoDB. Say each JSON unit that is served contains data about a film. I'd like my users to tick whether they've seen a film or not. My problem is that, being a beginner, I am not sure how this should be implemented.
I somehow need to save this for each user. Right now my mongo database just holds all of the films in general.
Could you give me an example of how this would be accomplished in a no-sql database? What if I want to save more detailed data like when a user takes private notes related to a certain film?
You can simply have a table that has the user_id, the movie_id and the boolean, which mean if there is movie_id and a user_id the boolean is true if not it remains false thats how i would impplement it.
You can have movies data some what like this which contains unique ID for each movie
movie={
id : <MOVIE_ID> //some movie ID
name : <MOVIE_NAME> //some name
... //other info you want to store
}
then you can save each movie that user had watched in his user data in a array
user = {
id : <USER_ID> //some user id
name : <USER_NAME> //some name
watched_movies : [<MOVIE_ID1>,<MOVIE_ID2>,.....] //movie IDS
... //other data about user
}
while you rendering all movies for particular user you just need to traverse these array and mark check for movie if user have already watched that movie
In a sails project, considering a model User and a model Role, with a relationship between User and Role :
// `User.js
module.exports = {
attributes: {
...
roles: {
collection: 'role',
dominant: true
},
...
}
}
For the the database representation, sails/waterline will create following tables :
table user,
table role,
table like user_roles__role_roles_role to represent the collection
I know we can force the name for the models USER and ROLE
(with the property 'tablename' : http://sailsjs.com/documentation/concepts/models-and-orm/attributes).
But how can we force the name the relationship table ? (Especially this name is quite long and tends to exceed limit).
Assuming this is a two-way relationship, and the Role model has a users collection, Sails will expect a table named role_users__user_roles, which has the role id first, user id second.
Your example table name would require User to be dominant and would require the Role model to have an attribute named roles_role that is a User collection.
To create your own join table, you can use the through association method and add a new model that represents the relationship, perhaps UsersRoles, and specify the tableName in that model definition.
Examples of the through association:
sails
docs
similar question
gist from comments in that question
I have one collection, called "games" whose documents store the ids of the owners of games.
{
"owner" : "88878b6c25c167d"
}
{
"owner" : "88878b6c25c167d"
}
{
"owner" : "af565f77f73469b"
}
Then I have another collection called "users".
{
"id" : "af565f77f73469b"
}
{
"id" : "a881335e1d4cf17"
}
{
"id" : "aa3ce3f7767c46b"
}
{
"id" : "d19e52c0bd78bcb"
}
{
"id" : "88878b6c25c167d"
}
So the first query I do retrieves the owners of all the games and stores those values in an array.['88878b6c25c167d', '88878b6c25c167d', 'af565f77f73469b']
The second query I want to perform should retrieve the documents of the users with the corresponding IDs. Previously I had used this query:
db.users.find({'id':
{'$in': [
'88878b6c25c167d',
'88878b6c25c167d',
'af565f77f73469b'
]}})
Here's the problem with this: It does not return duplicate documents if a user is listed twice, as above. Instead I just get one. This breaks my application. How can I make sure that each owner returns a document?
MongoDB works perfectly fine --- it finds all user, whose id-s are contained in the array.
Do not know the broader context of your needs (maybe tell us what you want to achieve -- not what is wrong?), but if you want to have an association between games and users something like that may be suitable:
after retrieving collection of games; just create an auxiliary hash map (normal JS object) that for given owner id will return the array of its games.
retrieve info about users who are owners.
if you want to know, which games belong to particular user just pass her id to data structure from 1. and get the arrays with games.
Is it what you were looking for? Do you need help with 1.?