db.getCollection('Leave').find({},{_id:0 ,
Can_It_Be_carry_forwarded:1})
这在MongoDb客户端CMD中可以正常运行,但在下面的代码中却不能正常运行
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();
})
});
答案 0 :(得分:1)
您的问题是find方法,您缺少投影字段。
如果您只想检索Can_It_Be_carry_forwarded
字段,则需要以下内容:{projection:{Can_It_Be_carry_forwarded:1, _id: 0}}
作为第二个参数。
类似问题的解决方案:https://stackoverflow.com/a/48294672/4120554
文档:http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#find
尝试一下:
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();
})
});