简体   繁体   中英

How to get all data matched each record from mongodb using mongoose and nodejs

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
     }
    });

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
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM