如何在mongodb本地驱动程序中对find()进行字段select?
我为node.js使用了mongodb本地驱动程序,无法使用字段select。 我想要做的是限制字段的名称。 我不想要输出中的“最后”。
我正在这样做:
db.collection("test").find({},[{'name':true,'last':false}]).toArray(function(err, results) { console.dir(results); });
但是日志打印:
[ { _id: 524b53588aa4f388de1c2ddb }, { _id: 524b53548aa4f388de1c2dda } ]
所以在输出中没有name
。
更新 :我已经尝试了一个对象,而不是数组 – 没有工作。 原因是混合包容和排斥。 你不能混用它。 当我只有"name":true
工作。
要find
的字段select参数是一个对象,而不是一个数组。 你不能混合字段包含和排除(除了_id
),所以它应该是:
db.collection("test").find({}, {'name': true}).toArray(function(err, results) { console.dir(results); });
省略数组:
db.collection("test").find({},{'name':true,'last':false}).toArray(function(err, results) { console.dir(results); });