简体   繁体   English

我如何从 mongodb 获取并显示所有没有未定义值的值?

[英]How can i get and display all value without undefined value from mongodb?

How can i get and display all value without undefined value from mongodb?我如何从 mongodb 获取并显示所有没有未定义值的值?

app.post('/add_teacher',(req,res)=>{
    const name = req.body.name;
    const biography = req.body.biography;
  teacherCollection.insertOne({name,biography})
         .then(result =>{
           console.log(result);
             res.send(result.insertedCount>0);            
        })                  
    }) 

app.get('/get_all_teacher', (req, res) =>{

  teacherCollection.find()
  .toArray((err, course) =>{
   res.send(course);
  })
})

suppose,i give a value for name but don't give value for biography.so the value of biography will be "undefined".now i want to get nothing where value is "undefined".I have used mongodb假设,我给名字一个值,但不给传记值。所以传记的值将是“未定义的”。现在我不想在值是“未定义的”的地方得到任何东西。我用过 mongodb

https://i.stack.imgur.com/39qHH.jpg https://i.stack.imgur.com/39qHH.jpg

In javascript when you want to access a key from an object and the key is not exists in object it return undefiendjavascript ,当您想要从object访问key并且该keyobject中不存在时,它返回undefiend

const biography = req.body.biography; // return undefiend if not exists

if you want null value or a default value when the key is not exists, this code will help you如果您想要null值或key不存在时的default值,此代码将帮助您

const biography = req.body.biography || null; // return null if not exists

const biography = req.body.biography || ''; // return empty string if not exists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能得到MongoDB这个数组的值呢? - How can I get the value of this array in MongoDB? 如何通过 mongodb nodejs 的平均评分连接产品获得价值? - How can I get value with join of product with average rating from mongodb nodejs? 我怎样才能在这种查询中从我的收藏中获得最小值和最大值 mongodb - how can i get min and max value from my collection in this kind of query mongodb 如何从嵌套数组中获取字段和值并查询它们以在 mongodb 中查找文档? - how can I get field and value from nested array and query them to find doc in mongodb? 我如何获得(Node.js)具有不同值的MongoDB文档,而其他值最低? - How can I get (Node.js) MongoDB documents with a distinct value where some other value is the lowest? 如何从 nodejs 中的 mongodb 获取特定值 - How to get specific value from mongodb in nodejs 如何从MongoDB文档中获取价值 - How to get value from a MongoDB document 如何从mongodb聚合获取值 - How to get a value from mongodb aggregate 当我使用 Nodejs 中的 mongodb 引擎向 MongoDB 数据库发出请求时,我得到未定义的值 - I get undefined value when I make a request to the MongoDB database using the mongodb engine in Nodejs 我可以显示MongoDB集合中的所有项目,但无法保存到其中:“未定义” - I can display all items in my MongoDB collection, but can't save to it: “undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM