2

This is an example of document that I have in my MongoDB:

{
    "_id": ObjectId('5525039895884d66710d0fc3'),
    "prid": "63527",
    "data": {
        "sku": "HF22-81639",
        "name": "Product Test",
        "ean": "8763900872512",
        "description": "This product is my first test",
    }
}

I want to make several search methods, where the search criteria are search by SKU, EAN or PRID. I created the methods but do not work, this is the example of one of the methods that I created and it does not work, just find the first document of my database but without any search criteria.

This search for "_id" if it works perfectly:

// GET - "_id"
app.get("/:id", function(req, res, next) {
    req.collection.findOne({
        _id: id(req.params.id)
    }, function(e, result) {
        if(e) return next(e);
        res.send(result);
    });
});

This search for "sku" does not work (this is where I need help):

// GET - "sku"
app.get("/sku/:id", function(req, res, next) {
    req.collection.findOne({
        sku: id(req.params.sku)
    }, function(e, result) {
        if(e) return next(e);
        res.send(result);
    });
});
Despotars
  • 541
  • 1
  • 8
  • 23

1 Answers1

2

Not sure how your id() function is defined but you could try:

// GET - "sku"
app.get("/sku/:id", function(req, res, next) {
    req.collection.findOne({
        "data.sku": req.params.id
    }, function(e, result) {
        if(e) return next(e);
        res.send(result);
    });
});

In your original code, req.params.sku is undefined because the req.params object doesn't have the field sku. From the url, only the req.param.id field is defined (from this => "/sku/:id"). So for example, if you test your API with this url:

http://localhost:3000/sku/HF22-81639

will bring back the document:

{
    "_id": ObjectId('5525039895884d66710d0fc3'),
    "prid": "63527",
    "data": {
        "sku": "HF22-81639",
        "name": "Product Test",
        "ean": "8763900872512",
        "description": "This product is my first test",
    }
}
chridam
  • 100,957
  • 23
  • 236
  • 235
  • 1
    Thank you very much, this is the best solution!! But, I have a question, if I have a field that is description, and I want to look up a word in the text in each product? How would it be? – Despotars Apr 08 '15 at 13:32
  • @Despotars Can you be a bit more clearer please? An example would be nice. From how I understood it, do you mean, say for instance, you want to search the collection for documents where the description field contains the product name, e.g `db.collection.findOne({"data.description": /productname/i})`? – chridam Apr 08 '15 at 13:37
  • yes sorry. For example looking for all the products they have in their description the word "park."And send the word as in the above example I send the sku – Despotars Apr 08 '15 at 13:43
  • @Despotars Can you please create another question for this since it would would be pretty noisy to answer in comments? I can't edit the my answer to accommodate this because it's a different question. – chridam Apr 08 '15 at 13:50
  • this is the new question: http://stackoverflow.com/questions/29517065/finding-a-mongodb-document-through-a-word-in-a-field-description-in-each-product – Despotars Apr 08 '15 at 14:15