Mongodb database design (product proposal)

I am planning to develop a module for my site. Similar to www.tastekid.com

I have users and products on my site.

For example; first users liked 2,4,56,57 products (numbers are product identifiers). The second (user) liked 1,4,56,57 products.

I should be able to search for who liked 56 and 57 and see what are the common product IDs for those users? in this case it is 4. then 1 or 2.

How can I create this? links or attachments? Or any other solutoin I don't know ...

I hope this clears up my problem. Thank.

+3


source to share


1 answer


Mongolian schema design documentation should give you a good starting point: http://www.mongodb.org/display/DOCS/Schema+Design There is a section on nesting and linking, and an example that contains implementations of both.

Here's another question asked in the Stack Overflow section on the topic of embedding and binding, which has a very detailed answer: MongoDB relationships: embedding or reference?

Mongolian documentation titled "Updating Data in Mongo" also contains sections on "Document and Reference Document Attachments": http://www.mongodb.org/display/DOCS/Updating+Data+in+Mongo

The above links should provide a very good understanding of the difference between nesting and document links and give you some good ideas for your schematic project.

In your specific example, it looks like you are looking to find users who love the same products. One possible solution is to include an array of product IDs that every user likes, for example.

> db.users.find()
db.users.save({ "_id" : 1, "name" : "Joe", "LikesProducts":[2,4,56,57] });
db.users.save({ "_id" : 2, "name" : "Jane", "LikesProducts":[1,4,56,57] });

      



To find which users love both 56 and 57 products, you can run the following query:

> db.users.find({LikesProducts:{$all:[56,57]}})
{ "_id" : 1, "name" : "Joe", "LikesProducts" : [ 2, 4, 56, 57 ] }
{ "_id" : 2, "name" : "Jane", "LikesProducts" : [ 1, 4, 56, 57 ] }
> 

      

More information on the $ all operator can be found in the Additional Queries documentation: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24all

Your application can then determine which other products (if any) appear in the LikesProducts arrays for both users.

Hopefully this will give you some ideas to consider in your application. If you have further questions, the Community is here to help!

+3


source







All Articles