Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB query to display all the fields value, except _id
Let us create a collection with documents −
> db.demo590.insert([
... { "Name": "Chris", "Age": 21 },
... {"Name": "Bob", "Age": 20},
... { "Name": "Sam", "Age": 19 }
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
Display all documents from a collection with the help of find() method −
> db.demo590.find();
This will produce the following output −
{ "_id" : ObjectId("5e92d514fd2d90c177b5bcd0"), "Name" : "Chris", "Age" : 21 }
{ "_id" : ObjectId("5e92d514fd2d90c177b5bcd1"), "Name" : "Bob", "Age" : 20 }
{ "_id" : ObjectId("5e92d514fd2d90c177b5bcd2"), "Name" : "Sam", "Age" : 19 }
Following is the query to display all the fields value, except _id −
> var listOfObject = [];
> db.demo590.find().forEach(function(d){
... var ob = {};
... ob["StudentFirstName"] = d.Name;
... ob["StudentAge"] = d.Age;
... listOfObject.push(ob);
... });
Following is the query to display the documents −
> listOfObject;
This will produce the following output −
[
{
"StudentFirstName" : "Chris",
"StudentAge" : 21
},
{
"StudentFirstName" : "Bob",
"StudentAge" : 20
},
{
"StudentFirstName" : "Sam",
"StudentAge" : 19
}
]Advertisements