Reputation: 103
I am currently working on a query that returns the names and positions of all employees in a database.
This is current query that I have come with:
db.transport.find({},{"EMPLOYEE.name":1,"EMPLOYEE.position":1, _id: 0}).pretty()
This is output it gives:
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ }
{ "EMPLOYEE" : { "name" : "John Smith", "position" : "driver" } }
{ "EMPLOYEE" : { "name" : "Peter Taylor", "position" : "mechanic" } }
{ "EMPLOYEE" : { "name" : "John Doe", "position" : "driver" } }
{ "EMPLOYEE" : { "name" : "John Gray", "position" : "mechanic" } }
{ "EMPLOYEE" : { "name" : "Adam Taylor", "position" : "driver" } }
{ "EMPLOYEE" : { "name" : "Michael Jones", "position" : "mechanic" } }
{ "EMPLOYEE" : { "name" : "Frederic Jones", "position" : "driver" } }
{ "EMPLOYEE" : { "name" : "Peter O'Brien", "position" : "mechanic" } }
{ "EMPLOYEE" : { "name" : "John Lucas", "position" : "driver" } }
{ "EMPLOYEE" : { "name" : "John Fox", "position" : "mechanic" } }
Type "it" for more
> it
{ "EMPLOYEE" : { "name" : "Adam Fox", "position" : "driver" } }
{ "EMPLOYEE" : { "name" : "Phillip Cox", "position" : "mechanic" } }
{ "EMPLOYEE" : { "name" : "Andrew K. Smith", "position" : "driver" } }
{ "EMPLOYEE" : { "name" : "Andrew R. Smith", "position" : "mechanic" } }
{ "EMPLOYEE" : { "name" : "Michael Potter", "position" : "driver" } }
How can I get rid of all the empty {} above the desired entries? I am aware that the {} are formerly the object ID for another table within the database.
Database for reference: https://pastebin.com/1PzQbru6
Am sorry if I cannot paste the contents of the database here as it exceeds the character limit
Upvotes: 0
Views: 42
Reputation: 1615
Try this -
db.transport.find({ "EMPLOYEE": { $exists: true } },{"EMPLOYEE.name":1,"EMPLOYEE.position":1, _id: 0}).pretty()
Upvotes: 1