Node.js collection.findOne({query})不应该返回文档本身吗?

Node.js collection.findOne({query})不应该返回文档本身吗?,node.js,mongodb,Node.js,Mongodb,我看到很多教程/指南使用collection.findOne{query}.field来获取返回文档中的字段值,但对我来说,这似乎不起作用,我想知道为什么。不过我确实想出了另一种方法。见下文: var rank = function(id) { // My way of doing it collection.findOne({ _id: id }, function(err, doc) { console.log(doc.score); // Prints the actual

我看到很多教程/指南使用collection.findOne{query}.field来获取返回文档中的字段值,但对我来说,这似乎不起作用,我想知道为什么。不过我确实想出了另一种方法。见下文:

var rank = function(id) {
  // My way of doing it
  collection.findOne({ _id: id }, function(err, doc) {
    console.log(doc.score); // Prints the actual score
  });

  // How some tutorials do it
  var score = collection.findOne({ _id: id }).score;
  console.log(score); // Gives a TypeError (Cannot read property 'score' of undefined)
};
//有些教程是如何做到这一点的


这些教程可能使用mongodb的shell,而不是node.jsAPI。shell的api看起来很相似,都是相同的词,findOne,等等,但它不使用回调。Shell的findOne会以内联方式返回文档。

听起来很合理。我想一定是这样。非常感谢。