如何在mongodb本机驱动程序的find()上进行字段选择? [英] How to do field selection on find() in the mongodb native driver?

查看:71
本文介绍了如何在mongodb本机驱动程序的find()上进行字段选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将mongodb本机驱动程序用于node.js,但无法进行字段选择.我想做的就是限制字段的名称.我不希望输出中的最后一个".

I am using the mongodb native driver for node.js and can't get to work the field selection. What I want to do is to limit fields to name. I do not want the 'last' in the output.

我正在这样做:

db.collection("test").find({},[{'name':true,'last':false}]).toArray(function(err, results) {
    console.dir(results);
});

但是日志显示:

[ { _id: 524b53588aa4f388de1c2ddb },
  { _id: 524b53548aa4f388de1c2dda } ]

因此输出中没有name.

更新: 我尝试了一个对象而不是数组-没用.原因实际上是混合包含和排除.你不能混.当我只有"name":true时,它起作用.

Update: I have tried an object instead of array -- did not work. The reason is really mixing inclusion and exclusion. You can't mix it. When I only had "name":true it worked.

推荐答案

find的字段选择参数是一个对象,而不是数组.而且您不能混合使用字段包含和排除(_id除外),因此应该是:

The field selection argument to find is an object, not an array. And you can't mix field inclusion and exclusion (except for _id), so it should be:

db.collection("test").find({}, {'name': true}).toArray(function(err, results) {
    console.dir(results);
});

这篇关于如何在mongodb本机驱动程序的find()上进行字段选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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