1

I'm trying to create an IOT device where it sends geographic data(coordinates) in real time. I have create a server using Nodejs,express,mongoose . I need to check if coordinate exist while saving the coordinated data. It means when i save the coordinate data it should send True or False as response if the coordinates exist in the database. Any idea on how to achieve this? Here is what i have tried but it take 2 calls to retrieve the data. I need to save the record as well getting the true/false response from saving the data.

   geo.create({
        package:req.body.package,
        longitude: req.body.longitude,
        lattitude: req.body.lattitude},
        function(err,data){
        if (err) throw err;

        geo.find({longitude: req.body.longitude,lattitude: req.body.lattitude})
          .then((data)=>{
           res.send("true");
          })
          .catch((err)=>{
            console.log(err);
         })
    });
TRomesh
  • 4,323
  • 8
  • 44
  • 74
  • Not "directly" related to the question, but I suggest you also read [`$near`](https://docs.mongodb.com/manual/reference/operator/query/near/) in the documentation, since the current storing of this data is not what you need for "nearest" queries should ( or rather "when" ) you want them – Neil Lunn Nov 12 '18 at 05:38

0 Answers0