How do I describe a collection in Mongo?

前端 未结 12 627
终归单人心
终归单人心 2021-01-31 02:32

So this is Day 3 of learning Mongo Db. I\'m coming from the MySql universe...

A lot of times when I need to write a query for a MySql table I\'m unfamiliar with, I woul

12条回答
  •  执念已碎
    2021-01-31 02:46

    If you are using NodeJS and want to get the all the field names using the API request, this code works for me-

    let arrayResult = [];
    
    db.findOne().exec(function (err, docs)){
     if(err)
      //show error
    
      const JSONobj = JSON.parse(JSON.stringify(docs));
         for(let key in JSONobj) {
           arrayResult.push(key);
         }
      return callback(null, arrayResult);
    }
    

    The arrayResult will give you entire field/ column names

    Output-

    [
     "_id",
     "emp_id",
     "emp_type",
     "emp_status",
     "emp_payment"
    ]
    

    Hope this works for you!

提交回复
热议问题