Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print document value in mongodb shell

I want to print the value for this JSON document in the mongo shell. Like a simple console output, without creating a new collection or document.

enter image description here

Thanks in advance

like image 790
kchromik Avatar asked Aug 27 '14 12:08

kchromik


People also ask

How do I fetch a document in MongoDB?

If we want to fetch all documents from the collection the following mongodb command can be used : >db. userdetails. find(); or >db.

How do I display records in MongoDB?

If you want to check your databases list, use the command show dbs. Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it. In MongoDB default database is test.

How do I display a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

What is print JSON in MongoDB?

You can use printjson() method to print to console an object in a MongoDB script. The syntax is as follows − printjson({yourFieldName”:yourValue”,........N}); You can use JSON.stringify() along with print() function.


1 Answers

I found a solution, by using .forEach() to apply a JavaScript method:

db.widget.find(
    { id : "4" }, 
    {quality_level: 1, _id:0}
).forEach(function(x) { 
    print(x.quality_level); 
});
like image 60
kchromik Avatar answered Nov 07 '22 18:11

kchromik