How to select a single field for all documents in a MongoDB collection?

前端 未结 20 1701
执念已碎
执念已碎 2020-11-22 07:58

In my MongoDB, I have a student collection with 10 records having fields name and roll. One record of this collection is:

{
    \"         


        
20条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 08:28

    For better understanding I have written similar MySQL query.

    Selecting specific fields 
    

    MongoDB : db.collection_name.find({},{name:true,email:true,phone:true});

    MySQL : SELECT name,email,phone FROM table_name;

    Selecting specific fields with where clause
    

    MongoDB : db.collection_name.find({email:'you@email.com'},{name:true,email:true,phone:true});

    MySQL : SELECT name,email,phone FROM table_name WHERE email = 'you@email.com';

提交回复
热议问题