使用 node.js 查询 mongoDB 的特定字段 [英] Query specific fields of mongoDB using node.js

查看:73
本文介绍了使用 node.js 查询 mongoDB 的特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,查询为我提供了所有字段.我只想查询 _id 和 serialno.如何去做.

In the code below the query gives me all the fields. I only want to query _id and serialno. How to go about it.

架构

var DataSchema = new Schema({
  serialno: String,
  info: String,
  active: Boolean,
  content: String
});

查询

// Get list of datas
exports.index = function(req, res) {
  Data.find(function (err, data) {
    if(err) { return handleError(res, err); }
    return res.json(200, data);
  });
};

推荐答案

如果您使用的是最新的 nodejs mongodb 驱动程序 3.0 或更高版本,请尝试以下代码:

If you are using latest nodejs mongodb driver 3.0 or above try this code:

Data.find({}).project({ _id : 1, serialno : 1 }).toArray()

这篇关于使用 node.js 查询 mongoDB 的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆