Using ObjectIds for relationships? MongoDB - node.js

I'm just putting my first steps in apps with MongoDB, and I'm not quite sure if what I'm doing is good practice or not.
I'm trying to do relationships between two collections. Let's say those will be User and User_settings collections. What I was going to do is adding field named user_id, that would contain obviously ID of document in User collection. And here come my concerns:
Should I use for it string format, or ObjectId format? I'm not sure it feels quite right to use ObjectId format. And that's because I experienced alot of problems while using this format in nodeJS apps. I had to convert objectId into string, in order to receive result I was looking for. Also, SailsJS is not allowing me to create new record with ObjectId fields. So my question is, can I use multiple ObjectIds in one record? I know I should do some googling, and I did. I'm just looking for 1-0 answer.
Thank you so much!

Related

Mongo Collection or Document

I'm looking at the possibility of switching from Filemaker to a MERN app. I get the basic idea of NoSQL databases but its a very different way of thinking. I have recreated what I need in MySQL, but Mongo DB (or Firestore) interests me.
The app is used to record workouts of clients. Each person has many workouts, each workout has many items within it. I'm not sure the best way to do this in Mongo.
I'm between using a collection for all workouts, and each workout has an array of all the items (exercises) they did. Or have the items as a separate collection with a workout_ID to link them up.
Probably the most important thing I'm looking for here is the ability to see all the instances someone did an exercise. From my understanding, if I do it the first way, every single record will be pulled. But the second would only return the documents in that collection that would match exercise_ID and person_ID.
Which is the best approach for such a project?
The second option is the best way to store the data in your case. You can always separate the items in a different collection and store the item Ids in your main collection i.e workouts here.
If you are concerned about querying(joins) them, mongoose provide 'populate' to fetch data between multiple collections using ids as references.
Or you can also use aggregate queries to achieve the same.
hope this helps :)
I think the best option for me would be having an array of the items inside each workout, because that's basically the purpose of using no-SQL databases, to access faster to the data.
However there's a basic rule.. if you think the array of items of each workout it's gonna grow very fast and it will get so big.. then it's better to have a separate collection for the items. if it's not.. then keep those inside an array.
Hope this can be helpful to you.. greetings! :)

MongoDB Database schema for ecommerce

Im currently building a multi User ecommerce app, (like shopify but for another sector). Im using nodejs with mongoDB. What is the best practice to store the orders?
Im currently have this schema:
UserModel{
username,
password,
ownsStore:}
StoreModel{storeID,
owner:,
products:[productarray],
categories:[],
orders:[array with all OrderModel{
orderid:
oderitems}]}
Will the Orders array in the store get to big?
Is it better Practice to put the Orders in a own colloction and assign them to the stores by using find and only saving the ObjectId in the order array of the store?
This schema will create you problems when the data will get bigger. Instead of doing this you should create different object for each order and map them with any unique ID of user.
The problem in this will come when you would like to sort various orders then you have to either use aggregation query which will take more time as compared to normal query or you will have to manually sort orders by using forEach or map functions.
Secondly you will also face problems while updating document if nested arrays comes into play because mongoDB does not provide support for deeply nested arrays so you would have to set again and again the value of array by manually updating array.
If you make different objects then all those things would get easier and faster to do.
I have worked on ecommerce project and have experienced the issues, so I later changed them to different object for each order.
So define a different schema for orders and add a unique ID of user in each order, so that the mapping becomes easy

Mongo Schema for Quiz Site

I'm building a small Node/Mongo app that serves users with up to 3 questions per day. Users can only answer yes or no and the correct answer will be determined at a later time (these questions are closer to predictions). Currently, I have these documents:
User
id
Question
id
QuestionAnswer
id
question_id (ref)
UserAnswer
id
question_id (ref)
user_id (ref)
What is the most efficient way to query the db so I get today's questions but also check whether that user has answered that question already? I feel like I'm overthinking it. I've tried a couple ways that seem to be overkill.
It's good to put them all in one schema since we don't have joins in mongodb.
It is faster than using relations.
Also for keeping your query small, take a look at this.
You should stay away from relations till you have a good reason for using them. So, what you need is only one schema.

creating and querying a collection in another collection. mongodb node.js

Is there a way to create and query a collection thats inside another collection...
Can any one direct me to literature that can teach me how to do this?
I've checked the mongodb
docs tutorial and i havent come across this scenario
No code example because i don't know where to start.
Here is what im trying to do.
Im modelling an online shop. So shop object details are in a collection holding many shops.
Now each shop has got products and each product its own unique details. I want to a void using references and rather append but from tutorial from mongodb docs its not quite clear how to do it.
Im fairly new to mongodb
Thanks.
There isn't any such thing as a collection inside another collection with mongo. A collection is a collection of documents.
There are a couple of ways to link documents to other documents, or collections of other data. The correct one to choose is up to you, your data, and your application.
Any document can point to another ObjectId from any field
You can have an array inside a document
Combine the two, and you can have an array of document ids
Mongo is a document store, not a relational database. My suggestion is don't try to use it as a relational database. If what you need is an RDBMS, use one.
Arrays:
http://docs.mongodb.org/manual/reference/operator/query-array/
http://docs.mongodb.org/manual/reference/operator/update-array/
ObjectId:
http://docs.mongodb.org/manual/core/document/

Mongoose for product system with different attributes depending on product type

I'm building a small product system to try and get me into Node.js, Mongo and the like.
I was drawn to Mongo for the possibilities of storing products of different types in the same collection instead of having multiple tables, eav or any of the other MySQL-methods that didn't quite fit.
I was reading through tutorials and at first fell over writing a Provider for the ProductModel, but then someone suggested i should use Mongoose.
However, the issue with Mongoose seems to be that i need a set schema for each model. My first idea was that a product should have a title and a type attribute. Then type would decide the overall product structure. So for example a dvd product might have a director while a t-shirt product would not have a director but would have different sizes.
Is it possible to implement this kind of structure in Mongoose, and how? It's my understanding that if i provide a schema for each produt type i won't be able to query them simultaneously? Does different schemas mean different collection? - I couldn't quite figure that out.
Any help and guidance is appreciated, would like to be able to use a framework instead of writing the DB handling myself (although that what was i planning to do initially, but if someone else allready did it better than me :))
Mongoose has a schemaless type which can be very useful in your case, but might not be the best solution.
ProductSchema = new Schema({any: {}});
If you change anything on this object you need to notify Mongoose about it with obj.markModified('field-name') before saving.
My suggestion is to use your own wrapper over node-mongodb-native, which I'm running on production for 7~8 months without problems.
There's another lib called mongous that you can check it out.

Resources