0

Is self generated object_id in mongo's collection unique across all the collections? Assuming my book_category collection, I have to put user_id to get all of the user's saved book category.

But how about update the data of book_category? should I put user_id inside my book category collection too?

module.exports.updateCategory = function(category_id,category,callback){
    var update = {
        name: category.category_name
    }

    Product_category.findOneAndUpdate({"_id":category_id},update,callback);
}

If the id is unquie accross the entire of my app then above code work fine. What will you do in my case?

Thian Kian Phin
  • 921
  • 3
  • 13
  • 25
  • @RobertMoskal can you give ur opinion on my case? – Thian Kian Phin Dec 27 '15 at 15:28
  • From the code you posted, I don't see where any cross collection issues come up. You select a document in a collection by it's id and update a field. – Robert Moskal Dec 27 '15 at 15:44
  • @RobertMoskal just imagine it's a library system, each user have books and book_category, will you store user_id within book_category? and when you want to update something, will you do double check? like check user_id and obj_id. – Thian Kian Phin Dec 28 '15 at 13:50

1 Answers1

0

Each book has an id and a user id, but the id is enough to uniquely identify the book. You have two common choices with book category:

It can be a collection that consists of a book id and a category id as well the id by which the book category is uniquely identified. The category id points to some other collection of categories. If you wanted all the categories for a book, you query by book id. If you want all the books for a category, you query by category id

Depending on your use cases, you could also simply hand an array of category ids from each book.

In neither case do I see any reason to involve the user id. Nor do I see any possibility of cross-collection id collision.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86