Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I am trying to save an array of strings into my Database using Mongoose.

So for that, I have declared a variable of a person schema:

var newPerson = new Person ({

    tags: req.body.tags

});

The schema itself looks like:

var personSchema = new mongoose.Schema({

  tags: Array

});

For saving, refer the following command

newPerson.save(function(err) {

    //basic return of JSON

});

With the help of Postman, I send in an array in the body but when I check the database, it only displays one entry with the array as a whole, you can refer to the screenshot:

enter image description here

Any ideas what I have to do?

1 Answer

0 votes
by (108k points)

In mongoose you can pass an array of strings as follows:

var personSchema = new mongoose.Schema({

tags: [{

    type: String

}]

The Postman sends the 'array' as the string so for that you can simply verify by inspecting the kind of req.body.tags. So for that refer the following command:

console.log(typeof req.body.tags)

If the above command yields a string data type then you have to set the type of the content from Postman to JSON.

If you want to know more about the working of MongoDB then do check out the MongoDB architecture which will help you in learning about the working of MongoDB. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...