0

I have a business collection.

The businesses are reviewed by users.

Each user can only review a business once. Future reviews will simply replace the former one.

This is how we store it:

 "Reviews" : {
    "gusyanto" : {
      "day" : "December 21, 2012, 8:08 am",
      "review" : "Tes review"
    },
    "sapi" : {
      "day" : "December 21, 2012, 8:18 am",
      "review" : "makanan ini sangat enak"
    }
  }

Alternatively we can also use this:

 "Reviews" : [{
      "userid" : "gusyanto",
      "day" : "December 21, 2012, 8:08 am",
      "review" : "Tes review"
    },
    {
      "userid" : "sapi",
      "day" : "December 21, 2012, 8:18 am",
      "review" : "makanan ini sangat enak"}
    ]
  }

So basically we want can store it as a dictionary where the key is the userid or we can store it as an array where userid is the field key. So the key in the second approach is static, namely userid.

We want to index the entries. For example, a user may want to know what businesses he has reviewed.

Which approach I should use?

1 Answer 1

1

AFAIK, MongoDB queries are more optimized for your second approach.

with the first approach you will probably have to do a query like:

db.businesses.find({'Reviews.gusyanto': {$exists: true}})

Which is less efficient than using:

db.businesses.find({'Reviews.userid': 'gusyanto'})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.