使用mongodb

时间:2016-08-05 12:22:24

标签: node.js mongodb database

在下面的代码中,我正在尝试记录单个已提取的文档但是记录了整个文档内容..可能是可能的解决方案..

  var findDocuments = function(db, callback) {
  var collection = db.collection('DATA');
  collection.find({'borough':'Bronx'}).toArray(function(err, docs) {
  assert.equal(err, null);
  console.log("Found the following records");
  console.log(docs);
  callback(docs);
 });      
}

output :
     [ { _id: 57a47c4a0d0c207a3e0efcc9,
         address: 
                { building: '1007',
                  coord: [Object],
                  street: 'Morris Park Ave',
                  zipcode: '10462' },
         borough: 'Bronx',
         cuisine: 'Bakery',
         grades: [ [Object], [Object], [Object], [Object], [Object] ],
         name: 'Morris Park Bake Shop',
         restaurant_id: '30075445' } 
    ]

1 个答案:

答案 0 :(得分:1)

要在使用查询查询时投影特定字段,请使用以下语法

collection.find({'borough':'Bronx'}, {borough:1}).toArray(function(err, docs) {
  assert.equal(err, null);
  console.log("Found the following records");
  console.log(docs); // will be array with only borough field and _id
  callback(docs);
 });