1

I'm new to MongoDB work and I need help in the following.

I have a collection named student in MongoDB. And I have the following data in it.

{'_id': 1, 'name': 'A', 'dt_joined': '2010'}
{'_id': 2, 'name': 'B', 'dt_joined': '2011'}
{'_id': 3, 'name': 'C', 'dt_joined': '2009'}
{'_id': 4, 'name': 'D', 'dt_joined': '2010'}
{'_id': 5, 'name': 'E', 'dt_joined': '2008'}

From the above collection, I want to retrieve (_id, dt_joined) from all the records. That means, I'm expecting the following result.

{'_id': 1, 'dt_joined': '2010'}
{'_id': 2, 'dt_joined': '2011'}
{'_id': 3, 'dt_joined': '2009'}
{'_id': 4, 'dt_joined': '2010'}
{'_id': 5, 'dt_joined': '2008'}

Is it possible with the MongoDB find command?

Thanks in advance!!

silpa
  • 57
  • 5

2 Answers2

1

try this

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

for more details check official site - https://docs.mongodb.com/manual/reference/method/db.collection.find/

Deepak
  • 1,373
  • 2
  • 10
  • 31
1

You can use one of the following.

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

db.student.find({}, {name: 0 })
Ashwanth Madhav
  • 1,084
  • 1
  • 9
  • 21