[英]Unable to check if field exists on MongoDB using NodeJs
I am using NodeJs and MongoDb as a backend service from my android app.I am checking if Phone field exists in my database. 我正在使用NodeJs和MongoDb作为我的Android应用程序的后端服务。我正在检查我的数据库中是否存在Phone字段。 My problem is everytime I check for the field it sends response field exists even if Phone field is not present in document. 我的问题是每次我检查它发送的字段响应字段是否存在,即使文档中不存在电话字段。
Here is my code: 这是我的代码:
const express = require('express');
const bodyParser = require('body-parser');
const env = require('dotenv').config();
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;
var dburl = process.env.URL;
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({extended:true}));
router.post('/checkPhone',(req,res) => {
var data = req.body.uId;
MongoClient.connect(dburl,{useNewUrlParser:true},(err,client) => {
if(err){
console.log("Error:" +err);
}
else{
var collect = client.db('Bookbudi_db').collection('Users');
collect.find({_id:data,Phone:{$exists:true}},(err,doc) => {
if(err){
console.log("Error:" +err);
}
if(doc){
res.send("Exist");
}
else{
res.send("Doesn't exist");
}
});
}
});
});
module.exports = router;
Someone please let me know what I am doing wrong in above code. 有人请告诉我上面的代码我做错了什么。 Any help would be appreciated. 任何帮助,将不胜感激。
THANKS 谢谢
if { $exists:true }
returns the document - it means that the field is there. if { $exists:true }
返回文档 - 这意味着该字段在那里。
It is possible that your schema defines this field and initiates it w/ null
- note that $exists
will still return those fields. 您的模式可能会定义此字段并使用null
启动它 - 请注意$exists
仍将返回这些字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.