How to select a single field in MongoDB?

2019-01-01 04:03发布

问题:

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

{
    \"_id\" : ObjectId(\"53d9feff55d6b4dd1171dd9e\"),
    \"name\" : \"Swati\",
    \"roll\" : \"80\",
}

I want to retrieve the field \"roll\" only for all 10 records in the collections as we would do in traditional database by using:

SELECT roll FROM student

I went through many blogs but all are resulting in a query which must have WHERE clause in it. For example:

db.students.find({ \"roll\": { $gt: 70 })

The query is equivalent to:

SELECT * FROM student WHERE roll > 70

My requirement is to find one field only without any condition. So, what is the query operation for that.

Thanks in advance.

回答1:

From the MongoDB docs:

A projection can explicitly include several fields. In the following operation, find() method returns all documents that match the query. In the result set, only the item and qty fields and, by default, the _id field return in the matching documents.

db.inventory.find( { type: \'food\' }, { item: 1, qty: 1 } )

In this example from the folks at Mongo, the returned documents will contain only the fields of item, qty, and _id.


Thus, you should be able to issue a statement such as:

db.student.find({}, {roll:1, _id:0})

The above statement will select all documents in the students collection, and the returned document will return only the roll field (and exclude the _id).

If we don\'t mention _id:0 the fields returned will be roll and _id. The \'_id\' field is always displayed by default. So we need to explicitly mention _id:0 along with roll.



回答2:

I think mattingly890 has the correct answer , here is another example along with the pattern/commmand

db.collection.find( {}, {your_key:1, _id:0})

\"enter



回答3:

get all data from one field with _id

db.student.find({}, {roll:1})

SELECT roll FROM student

get all data from one field without _id

db.student.find({}, {roll:1, _id:0})

find specified data using where clause

db.student.find({roll: 80})

SELECT * FROM students WHERE roll = \'80\'

find a data using where clause and greater than condition

db.student.find({ \"roll\": { $gt: 70 }})

SELECT * FROM student WHERE roll > \'70\'

find a data using where clause and less than or equal to condition

db.student.find({ \"roll\": { $lte: 70 }})

SELECT * FROM student WHERE roll <= \'70\'

find a data using where clause and less than to condition

db.student.find({ \"roll\": { $lt: 70 }})

SELECT * FROM student WHERE roll < \'70\'



回答4:

Just for educational purposes you could also do it with any of the following ways:

1.

    var query = {\"roll\": {$gt: 70};
    var cursor = db.student.find(query);
    cursor.project({\"roll\":1, \"_id\":0});

2.

    var query = {\"roll\": {$gt: 70};
    var projection = {\"roll\":1, \"_id\":0};
    var cursor = db.student.find(query,projection);

`



回答5:

Try the following query:

db.student.find({}, {roll: 1, _id: 0}).pretty();

Hope this helps!!



回答6:

This works for me,

db.student.find({},{\"roll\":1})

no condition in where clause i.e., inside first curly braces. inside next curly braces: list of projection field names to be needed in the result and 1 indicates particular field is the part of the query result



回答7:

getting name of the student

student-details = db.students.find({{ \"roll\": {$gt: 70} },{\"name\": 1, \"_id\": False})

getting name & roll of the student

student-details = db.students.find({{ \"roll\": {$gt: 70}},{\"name\": 1,\"roll\":1,\"_id\": False})


回答8:

db.<collection>.find({}, {field1: <value>, field2: <value> ...})

In your example, you can do something like:

db.students.find({}, {\"roll\":true, \"_id\":false})

Projection

The projection parameter determines which fields are returned in the matching documents. The projection parameter takes a document of the following form:

{ field1: <value>, field2: <value> ... }
The <value> can be any of the following:
  1. 1 or true to include the field in the return documents.

  2. 0 or false to exclude the field.

NOTE

For the _id field, you do not have to explicitly specify _id: 1 to return the _id field. The find() method always returns the _id field unless you specify _id: 0 to suppress the field.

READ MORE



回答9:

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\';



回答10:

Here you go , 3 ways of doing , Shortest to boring :

db.student.find({}, \'roll _id\'); // <--- Just multiple fields name space separated
// OR
db.student.find({}).select(\'roll _id\'); // <--- Just multiple fields name space separated
// OR
db.student.find({}, {\'roll\' : 1 , \'_id\' : 1 ); // <---- Old lengthy boring way

To remove specific field use - operator :

db.student.find({}).select(\'roll -_id\') // <--- Will remove id from result


回答11:

db.student.find({}, {\"roll\":1, \"_id\":0})

This is equivalent to -

Select roll from student



db.student.find({}, {\"roll\":1, \"name\":1, \"_id\":0})

This is equivalent to -

Select roll, name from student



回答12:

Use the Query like this in the shell:

1. Use database_name

e.g: use database_name

2. Which returns only assets particular field information when matched , _id:0 specifies not to display ID in the result

db.collection_name.find( { \"Search_Field\": \"value\" }, 
                  { \"Field_to_display\": 1,_id:0 }  )


回答13:

In mongodb 3.4 we can use below logic, i am not sure about previous versions

select roll from student ==> db.student.find(!{}, {roll:1})

the above logic helps to define some columns (if they are less)



回答14:

If u want to retrieve the field \"roll\" only for all 10 records in the collections. Then try this.

In MongoDb :

db.students.find( { } , { \" roll \" : { \" $roll \" })

In Sql :

select roll from students