Answers for "how to get a specific field of collection mongo db"

Go
4

mongodb select specific fields

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
Posted by: Guest on December-07-2020
0

FIND SPECIFIC FIELDS FROM SunDOCUMENTS OF ALL DOCUMENTS UNDER A COLLECTION IN MONGODB

//use when you want `_id,size` (As object) with its only property `h` in 
//$project,

db.inventory.aggregate([{$match:{  item: /^j/  } },{ $project:{ 
"size.h":"$size.h" } }])

//use when you want `_id,size` size ase whole object  
//$project,

db.inventory.aggregate([{$match:{  item: /^j/  } },{ $project:{ "size":1 } 
}])
Posted by: Guest on November-24-2021

Code answers related to "how to get a specific field of collection mongo db"

Browse Popular Code Answers by Language