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.
Thanks in advance
If we want to fetch all documents from the collection the following mongodb command can be used : >db. userdetails. find(); or >db.
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.
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With