0

Tried to get value of product_name from my mongodb using mongoose but i do not know how to do it.

My DB Data collection:

{
_id:ObjectId("5ecea02ebb6f3c19e86fe805"),
product_name:"Test1"
},
{
_id:ObjectId("5ecea02ebb6f3c19e86fe806"),
product_name:"Test2"
},
{
_id:ObjectId("5ecea02ebb6f3c19e86fe807"),
product_name:"Test3"
},
{
_id:ObjectId("5ecea02ebb6f3c19e86fe808"),
product_name:"Test4"
},
{
_id:ObjectId("5ecea02ebb6f3c19e86fe809"),
product_name:"Test5"
}

data.controller.js:

module.exports.getData = (req, res, next) => { 
var tableCate = mongoose.model("Product"); 
    tableCate.find({ product_name }, function(err, docs) {
    if (err) {
        console.log( err);
        return
    } else {
        console.log(docs)// output should be Test1,Test2,Test3,Test4,Test5
     }
    });

1 Answer 1

1

db.collection.find returns a Cursor which is A pointer to the result set of a query, to access the result you can use db.collection.find({}).toArray() to return an array of documents or

.forEach(function(item){
// and you can print or do what you want with each item
})
Sign up to request clarification or add additional context in comments.

2 Comments

How to get matched record..exmaple i want to check product_name ="test6" there or not?
You can use .forEach and inside you can check what you want by if(item.product_name=='test6'){console.log(item)}

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.