2014년 12월 28일 일요일

Mongo Data Model design for my data

Need to design a Mongo collection for the scenario.The application has two pages Dashboard - Users can save and new notes and view their latest notes and Search- Search for old notes of all users.
Every user can create note(s) based on VIN number.
All users can view their last two weeks notes in the application Dashboard;
Once the notes are two weeks old they are removed from the dashboard and users need to search the notes based on VIN number to view their old notes in search page.
All users can search other users notes based on the VIN number.
There are 1500 users and each individual note will be of maximum size of 6 KB and against a VIN there can be 0 -10 notes
So my question is whether the design should be, create a collection for every VIN number and store all the user notes against that VIN as arrays
       {
  "vin": "3H7REIN8803",
  "users": [{
    "user_id" : "kalyle@yeservice.com",
    "notes":[{
      "created_on": "12-12-2014",
      "note": "This vehicle owner is notified for free service .... "},
      {
      "created_on": "12-14-2014",
      "note": "Vehicle owner requested for adding rear spoilers.... "}
      ]},{
    "user_id" : "kalyle@yeservice.com",
    "notes":[{
      "created_on": "12-15-2014",
      "note": "This vehicle owner is notified for free service .... "}]

  }]
  }
or or creating a collection for every user and saving the user note as array
        {
  "user_id": "kalyle@yeservice.com",
  "vins": [{
    "vin" : "3H7REIN8803",
    "notes":[{
      "created_on": "12-12-2014",
      "note": "This vehicle owner is notified for free service .... "}
      ]},{
    "vin" : "783JDGH6338K",
    "notes":[{
      "created_on": "12-12-2014",
      "note": "This vehicle is having spark plug issues after service .. "}]

  }]
  }
Which is the best approach and is there any better implementation.
If I create the collections based on the user I will end up creating 1500 collections, but when i search based on VIN number I need to search 1500 collections for notes related to that VIN.
If I create colletion based on VIN number I will be having 'N' number of collections but the search notes based on VIN number will be easy as I just need to fetch that collection alone
For showing the users dahsboard (i.e their latest notes for last two weeks) the search will be extended to 'N' VIN collections
Can any one pull be out from thisDilemma Thanks in advance.



If I've understood your use case correctly, there are three fundamental operations:

  1. Dashboard: retrieve the last 2 weeks worth of notes for a given user (for all VIN)
  2. Search: retrieve all notes for a given VIN (regardless of user)
  3. Insert: create a new note for a given user with a given VIN
Based on this, the design that comes to mind is to have one collection, notes, where each document corresponds to a note:

{
    "_id" : ObjectId(...),
    "VIN" : "DEADBEEF88",
    "user_id" : "johnny.b.good@cars.gov",
    "created_on" : ISODate("2014-12-19T20:04:08.913Z"),
    "note" : "still has that new car smell!"
}

All of your core operations are easy (written in mongo shell syntax with variables subbed for values):
  1. Dashboard: db.notes.find({ "user_id" : user_id, "created_on" : { "$gte" : two_weeks_ago } }).sort({ "created_on" : -1 })
  2. Search: db.notes.find({ "VIN" : vin }).sort({ "created_on" : -1 })
  3. Insert: db.notes.insert({ "user_id" : user_id, "VIN" : vin, "created_on" : ISODate(), "note" : "adjust the tappets" })
An index on { "user_id" : 1, "created_on" : -1 } will make the first query fast. You could do a similar index for the second query, but if there's at most 10 notes per VIN it might not be worth it and you should only make an index on { "VIN" : 1 }.


댓글 없음:

댓글 쓰기