0

I am developing an application with mongodb and nodejs I should also mention that I am new to both so please help me through this question

my database has a collection categories and then in each category I am storing products in subdocument just like below :

   {
    _id : ObjectId(), 
    name: String,
    type: String,        
    products : [{
       _id : ObjectId(),
       name : String,
       description : String,
       price : String
    }]
});

When it comes to store the orders in database the orders collection will be like this:

 {
    receiver : String,
    status : String,
    subOrders : [
        {
            products :[{
                productId : String,
                name : String,
                price : String,
                status : String
            }],

            tax : String,               
            total : String,
            status : String,
            orderNote : String
        }
    ]
}

As you can see we are storing _id of products which is a subdocument of categories in orders when storing there is no issue obviously, when it comes to fetch these data if we just need the limited field like name or price there will be no issue as well, but if later on we need some extra fields from products like description,... they are not stored in orders.

My question is this: Is there any easy way to access other fields of products apart from loop through the whole categories in mongodb, namely I need a sample code for querying the description of a product by only having its _id in mongodb?

or our design and implementation was wrong and I have to re-design it from scratch and separate the products from categories into another collection?

please don't put links to websites or weblogs that generally talks about mongodb and its collections implementations unless they focus on a very similar issue to mine

thanks in advance

1 Answer 1

1

I'd assume that you'd want to return as many product descriptions as matched the current list of products, so first, there isn't a query to return only matching array elements. Using $elemMatch you can return a specific element or the first match, but not only matching array elements. However, $elemMatch can also be used as a projection operator.

db.categories({ "products._id" : "PID1" }, 
              { $elemMatch :  { "products._id" : "PID1" },
               "products._id" : 1, 
               "products.description" : 1})

You'd definitely want to index the "products._id" field to achieve reasonable performance.

You might consider instead creating a products collection where each document contains a category identifier, much like you would in a relational database. This is a common pattern in MongoDb when embedding doesn't make sense, or complicates queries and aggregations.

Assuming that is true:

You'll need to load the data from the second collection manually. There are no joins in MognoDb. You might consider using $in which takes a list of values for a field and loads all matching documents.

Depending on the driver you're using to access MongoDb, you should be able to use the projection feature of find, which can limit the fields returned for a document to just those you've specified.

As product descriptions ardently likely to change frequently, you might also consider caching the values for a period on the client (like a web server for example).

db.products.find({ _id: { $in : [ 'PID1', 'PID2'] } }, { description : 1 })
4
  • I think I was clear enough in my question and provided enough details actually there is no products collection in my db,.. There is a Categories and it has a subdocument which stores products and since I'm new to Mongodb I'm looking for the fastest query and most optimized one which fetches the product.description by only having product _id
    – Siavosh
    Feb 20, 2014 at 7:30
  • Sorry, I'd translated it mentally into a working structure. I've added a few more details explaining why your proposed structure won't work for your requirements. Feb 20, 2014 at 11:45
  • Thanks for updating the answer so your suggestion is to avoid more than one step nesting the elements
    – Siavosh
    Feb 22, 2014 at 8:33
  • Yes and no. It depends if products could be in more than one category. Then, I'd expect them to be stored separately. Feb 22, 2014 at 12:55

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.