Open In App

Mongoose Document.prototype.toString() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Document API.prototype.toString() method of the Mongoose API is used on the Document model. It allows to get the result in the form of string. Using this method we can convert the output in String type. Let us understand the toString() method using an example.

Syntax:

document.toString();

Parameters: This method does not accept any parameter.

Return Value: This method returns the value in String format.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this: 

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Example 1: In this example, we are illustrating the functionality of toString() method. We are fetching one document from the collection and converting its result into string using toString() method.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const Customer = connectionObject.model(
    "Customer",
    new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    })
);
  
Customer.find({ name: "Harshit" }).then(res => {
    console.log(res.toString());
}).catch(err => {
    console.log(err);
})


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

{
  _id: new ObjectId("63c1c0294a390b560860be13"),
  name: 'Harshit',
  address: 'Pune',
  orderNumber: 45,
  __v: 0
}

Example 2: In this example, we are illustrating the functionality of toString() method. At the end, we are verify the type of the result which we have converted into string using toString() method.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const Customer = connectionObject.model(
    "Customer",
    new mongoose.Schema({
        name: String,
        address: String,
        orderNumber: Number,
    })
);
  
Customer.find({ _id: "639ede899fdf57759087a653" }).then(res => {
    const stringData = res.toString();
    console.log(typeof stringData)
}).catch(err => {
    console.log(err);
})


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

string

Reference: https://mongoosejs.com/docs/api/document.html#document_Document-toString



Last Updated : 20 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads