Pregunta

I want to create a database for ecommerce web application using mongoDBalmost similar to it

I am not able to get the idea how to set primary key for it as for every entry it automatically create id which is unique so I can use it as primary key In mongoDB terminal

db.department.findOne({dep_name :"clothing"}) { "_id" : ObjectId("5277d82f658d9f107b7ae64e"), "dep_name" : "clothing" }

For the department clothing the dep_id is ObjectId("5277d82f658d9f107b7ae64e") and while creating category of men,women,kids the dep_id will remain same and at the same time other object id will get created which act as cat_id(unique for men,women,kids). I am not getting idea how to get dep_id where dep_name is clothing and insert the dep_id for the new collection which will have fields ObjectId(automatically created),dep_id,cat_name.

Apart from it if this seems not to be good idea for creating database for ecommerce web application then suggest what will be the best

¿Fue útil?

Solución

If I understand your question correctly you are asking how, for example, to insert a document in a "category" collection that contains a "dep_id" field containing the _id of a department such as "clothing"?

Here is an example of how you would do that using the mongo shell:

clothing = db.department.findOne({dep_name: "clothing"})
db.category.insert({dep_id: clothing._id, cat_name: "women"})

Does this answer your question?

Bruce

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top