mongo .find 只为所有用户返回特定字段 [英] mongo .find return specific field only for all user

查看:44
本文介绍了mongo .find 只为所有用户返回特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

db.users.find({}) 返回具有所有字段的所有用户.

db.users.find({}) returns all the users with all the field.

如何编写只为所有用户返回电子邮件"字段的请求?

How to write a request that would return only the 'email' field for all user ?

推荐答案

请看文档.您会注意到 find 有一个可选的第二个参数,称为 投影".此参数用于说明您需要哪些字段.要仅获取名为电子邮件"的字段,请使用此投影:

Please take a look at the documentation. You will notice that find has an optional second parameter called "projection". This parameter is used to state which fields you want. To get only the field named "email", use this projection:

db.users.find({}, { _id: 0, email: 1 })

这篇关于mongo .find 只为所有用户返回特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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