The Function PrintJson with Each () function and how it displays all the documents present in the collection in a JSON Format.The function Each () with find () function and how this function goes through from each and every document present in the collection.The following query has to be run on cmd: db.staff.find().forEach(printjson). How to print the output in JSON format.How could we use insert () command for insertion of multiple documents at a time?.If we put the whole document in a nutshell, the following are the pointers that we have covered. These are the steps through which we can make multiple documents get stored in mongo collections by using arrays in the insert () function. If the above command runs without error, then the following would be the output.Īs we see, the output is readable and is in the JSON format. This will display all the documents present in the collection in a JSON Format. The Function PrintJson is commanded for Each () function.It is done to mobilize the function to go through from each and every document present in the collection. The function Each () would be attached to the find () function.Syntax: db.staff.find().forEach(printjson) To print the output in JSON format, the following query has to be run on cmd. In the following example, we are going to fetch the record by staff ID, where the staff ID is equal three and it will return us only that document. This parameter will help us to fetch only that record which meets the criteria. In our next Example, we will use JSON print to see the output of the above query in a more readable format. In order to fetch a specific document from the MongoDB collection, we can use a query parameter in the find method. JSON is a format which is used to compile the information that has been stored in MongoDB into a standardized form which would make it more readable. Printing in the format of JavaScript Object Notation In this Node.js Tutorial – Node.js Mongoose – Connect to MongoDB, we have learnt to connect to MongoDB using Mongoose, get the reference to Database, check if the connection is successful or not.The given output displays that we have inserted 3 documents into the mongo collection. When the connection is made without any errors and is open, callback function provided to db.open(‘open’, callback) is executed. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:8986) DeprecationWarning: Unhandled promise rejections are deprecated. (node:8986) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Slash in host identifier If the connection is not successful, you may see the following error message displayed on console. nnect('mongodb://localhost:ab017/tutorialkart') To simulate ‘Connection not successful’ scenario, lets change the port to some incorrect value. Output $ node node-js-mongodb-connection.js nnect('mongodb://localhost:27017/tutorialkart') ĭb.on('error', (console, 'connection error:')) Node-js-mongodb-connection.js var mongoose = require('mongoose') To check if the connection is successful or not, you may use callback functions : on() and once(). To get a reference to the database specified, use connection() function on Mongoose reference, as shown below : var db = nnection Is connection successful ?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |