Intro:
I have a collection of players in my app, this is built using a schema and then passing some stuff into my node.js configuration, perhaps it's a bit more complex than that but I mean this is the basic idea of how mongoDB for nodejs works with models and collections.
So now in my database I have my DB name then collections then a collection of players, great now I can build an api and start making requests to GET and PUT from that collection.
Question:
My problem is in my stats app after week one's game was tracked I need to clear all the player attributes so that in week 2 it's fresh, but I would like to save all the stats for the players from week 1.
So due to my basic knowledge of how a collection is built I am thinking in order to build a new collection I need to define a schema, but perhaps there is another way, if not then the schema would have to be dynamic right? Well how do I build a dynamic schema to suit this problem?
Ideas:
Idea: The user clicks a button saying complete week 1, then the server takes all the data from the players collection and builds a
new collection named week 1 players?
Question: If the above is a good idea, how do I build a function to create a new mongoDB collection and save players_week1 into its
own branch?
Final Word:
So hopefully someone has an optimal solution and understands what I am talking about, in the mean time I am going to try and follow my idea and see if I can search the docs to find some answers. Thanks guys.
Related
for my school project i have to recreate Secret Hitler game using socket.io/express/vuejs2.
And at the moment I'm stuck at the part where i should create lobby that other people should be able to join. I used rooms for the lobby, but then i realized that it's not what i need. What i though of is when person creates a lobby, a name will be assigned to that specific lobby so other user will be able to join that lobby by inserting the name of it or using the link which would look something like that http://mygame.com/?lobby=ABCD. But if i got socket.io rooms correctly, i wont be able to do that, because you can't create unique rooms. Or I'm just dumb and haven't figured that out yet.
So my question is how could i implement lobbies based on my idea using socket.io and expressjs? What documentation should i read?
Try this Tutorial
https://www.youtube.com/watch?v=DvlyzDZDEq4
I know it is a zoom app but it will give you a basic idea on how to create rooms with node.js
I have a local container created with Docker with MongoDB & an express node server
What is the recommended way to populate it with new data?
1) Use the cli
2) Using Mongoose
3) Use a GUI such as Compass
Thanks!
I don't know if there's a 'correct' way to do this, but I've run a couple of 'seeds' files for my projects:
https://github.com/rmgreenstreet/surfShop/blob/master/seeds.js
https://github.com/rmgreenstreet/yelpcamp/blob/master/seeds.js
https://github.com/rmgreenstreet/custom-forms/blob/master/seeds.js
I almost wish there was some kind of niche field/need/position for being able to generate fake data!
The point is that you'll need to set and understand the structure of your data and essentially go through a bunch of nested loops for any connected/dependent data types.
Now if you're working with a SQL database, I'm totally clueless. That's next on my "things to learn" once I feel more comfortable with Javascript/NoSQL.
This would possibly depend on the usecase here,
Answer would be:
MONGOOSE : If you are planning to deploy an application using express. As mongoose goes hand in hand with express. (https://medium.com/#SigniorGratiano/mongoose-and-express-68994fcfdeff) As in many stacks like MEAN, MERN.
GUI like Compass: When you have to visualise the data or do ONE TIME OPERATIONS.
This question has been driving me crazy and I can't get my head around it. I come from a MySQL relational background and have been using Meteorjs and Mongo. For the purposes of this question take the example of posts and authors. One Author to Many Posts. I have come up with two ways in which to do this:
Have a single collection of posts - Each post has the author information embedded into the document. This of course leads to denormalization and issues such as if the author name changes how do you keep the data correct.
Have two collections: posts and authors - Each post has an author ID which references the authors collection. I then attempt to do a "join" on a non relational database while trying to maintain reactivity.
It seems to me with MongoDB degrees of denormalization is acceptable and I am tempted to embed as implementing joins really does feel like going against the ideals of Mongo.
Can anyone shed any light on what is the right approach especially in terms of wanting my app data to scale well and be manageable?
Thanks
Denormalisation is useful when you're scaling your application and you notice that some queries are taking too much time to complete. I also noticed that most Mongodb developers tend to forget about data normalisation but that's another topic.
Some developers say things like: "Don't use observe and observeChanges because it's slow". We're building real-time applications so that a normal thing to happen, it's a CPU intensive app design.
In my opinion, you should always aim for a normalised database design and then you have to decide, try and test which fields, that duplicated/denormalised, could improve your app's performance. Example: You remove 1 query per user. The UI need an extra field and it's fast to duplicated it, etc.
With the denormalisation you've an extra price to pay. You've to update the denormalised fields according to the main collection.
Example:
Let's say that you Authors and Articles collections. On each article you have the author name. The author might change his name. With a normalised scenario, it works fine. With a denormalised scenario you have to update the Author document name AND every single article, owned by this author, with the new name.
Keeping a normalised design makes you life easier but denormalisation, eventually, becomes necessary.
From a MeteorJs perspective: With the normalised scenario you're sending data from 2 Collections to the client. With the denormalised scenario, you only send 1 collection. You can also reactively join on the server and send 1 collection to the client, although it increases the RAM usage because of MergeBox on the server.
Denormalisation is something that it's very specify for you application needs. You can use Kadira to find ways of making your application faster. The database design is only 1 factor out of many that you play with when trying to improve performance.
Hi Stackoverflow Family,
So this is a pretty big question; be prepared to read quite a bit.
Basically my team and I working on an Employee scheduling application (we're already well into it, so please nothing about changing our stack or anything like that). Anyways, we're building this with PHP, Mongo, JavaScript, JQuery, and Bootstrap; and I'm working more on the db side of things.
What I really want to find out after asking this question is to see if my current approach to the db is right; I've basically created several collections and documents that reference each other in order to access or reference specific data that I'm looking for. If that doesn't really make sense check out my schema below:
Employee Collection - Contains indexes such as name, employeeNumber, address, etc, employee availability, position & department.
(My issue here is I want it to reference other collections which contain their Shift information, but I can only really do that when I insert a document).
Shift Collection - Contains indexes such as shiftNumber, shiftStartTime, endTime (This collection, I basically want to reference to employee, such that for creating each employee I have their shift time connected to it).
**Schedule Collection ** - Now for the Schedule collection, this is the one that confuses me the most; I basically want our Calendar UI to be able to look through our schedule collection and be able to pull all the shifts in a certain day, or in a specific week. But I have no idea as to how I can approach this from the backend.
So far what I've done with the Schedule Collection is that I've mathematically created a Calendar year and placed that within the Schedule; basically it contains a document called Year, and in that Year it contains every day of the week with information such as day number, week number, leapYear, etc.
Anyways, I hope this is enough information; my main confusion is the with the main schedule, I think I nearly have the Employee collection functioning properly since it references the Department class with no issues. I just mainly can't figure out how to implement a full schedule in mongo!
Thanks guys!
I am creating a mock app with user creation/auth/friend in a node js learning exercise. Having spent my time mostly at the front end of things, I am a n00b as far as DBs are concerned. I want to create a user database where I want to keep track of user profiles and their connections/friends.
Primary objective is to load/store users connections in the database.
Fetch this information and give it to the user most efficiently in least number of queries.
I'd really appreciate some help with a DB structure I should be using that can accomplish this. I am using mongodb and node.
Off the top of my head: I can store the user's connections in an object in the "connections" field. But this will involve making a lot of queries to fetch connections' details like their "about me" information - which I can also store in the same object as well.
Confused. Would really appreciate some pointers.
Take a look at the Mongoose ORM. It has a populate method that grabs foreign documents. Lots of other great stuff too.
You could say
Users.find({}).populate('connections').exec(function(err,users) { ... });
Before popualte the users' array of connections was an array of IDs, after, its an array of user documents.