限制find()中的字段在Mongodb中不起作用的第二个参数 [英] Second parameter to limit fields in find() not working in Mongodb

查看:160
本文介绍了限制find()中的字段在Mongodb中不起作用的第二个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mongodb数据库视图

我想在结果中省略_id字段

MongoClient.connect("mongodb://localhost:27017/",function(err,client){
    var db = client.db("customerDetails");
    db.collection("customerName").find({},{ _id : 0}).toArray(function(error,result){
        console.log(result);
        client.close();
    });
});

从w3学校中查找

但是以某种方式无法正常工作.我仍然在结果对象中得到_id字段. 我想念什么??

But somehow its not working. I still get the _id field in my result object. What am i missing ??

推荐答案

在mongodb驱动程序的3.0版中,find()的第二个参数是options对象,而不是投影文档.请参见文档此处.要发送投影文档,请设置选项文档的projection属性.例如

In version 3.0 of the mongodb driver, the second parameter to find() is the options object, not the projection document. See the documentation here. To send a projection document, set the projection property of the options document. E.g.

db.collection("customerName").find({}, { projection: { _id: 0 } })

或者,使用项目方法:

db.collection("customerName").find({}).project({ _id: 0 })

在mongodb驱动程序的2.2版中,find的第二个参数确实是投影文档.最新版本(3.0)对此进行了更改.因此,某些博客文章可能不是最新的.请参阅3.0更新日志的相关部分这里.

In version 2.2 of the mongodb driver, the second argument of find was indeed the projection document. This has changed in the latest version (3.0). So some blog posts might not be up to date. See the relevant section of the 3.0 changelog here.

这篇关于限制find()中的字段在Mongodb中不起作用的第二个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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