简体   繁体   中英

NodeJs And MongoDB

      db.getCollection('Leave').find({},{_id:0 , 
      Can_It_Be_carry_forwarded:1})

this is working perfectly in the MongoDb Client CMD But not in the Below Code

      var MongoClient = require('mongodb').MongoClient;
      var url = "mongodb://localhost:27017/Chatbot_Project";

      MongoClient.connect(url, function(err, db) {
         if (err) throw err;

         var dbo = db.db("Chatbot_Project");
         dbo.collection('Leave').find({}, {Can_It_Be_carry_forwarded:1}).toArray(function(err, result) {
            if (err) 
              throw err;

            console.log(result);
            db.close();
         })
       });

Your problem is the find method, you are missing the projection field. If you want to retrive only the Can_It_Be_carry_forwarded field you need the following: {projection:{Can_It_Be_carry_forwarded:1, _id: 0}} as the second argument.

Solution from a similar question: https://stackoverflow.com/a/48294672/4120554

Documentation: http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#find

Try this:

  var MongoClient = require('mongodb').MongoClient;
  var url = "mongodb://localhost:27017/Chatbot_Project";

  MongoClient.connect(url, function(err, db) {
     if (err) throw err;

     var dbo = db.db("Chatbot_Project");
     dbo.collection('Leave').find({},{projection:{_id: 0, Can_It_Be_carry_forwarded:1}}).toArray(function(err, result) {
        if (err) 
          throw err;

        console.log(result);
        db.close();
     })
   });

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