push array of strings to mongoDB with mongoose push array of strings to mongoDB with mongoose mongoose mongoose

push array of strings to mongoDB with mongoose


If the actions tags are predefined and specific you can use html select multiple. That way actions property will be sent as an array of strings

<form action="/blogs" method="POST">        <input type="text" placeholder="title" name="title" id="title" >         <label for="actions">Actions:</label>        <select name="actions" id="actions" multiple>            <option value="walking">walking</option>            <option value="smiling">smiling</option>            <option value="laughing">laughing</option>        </select>    <button >submit</button></form>

And inside the controller you handle this way

  // req.body.actions is already an array.  const blog = new Blog(req.body);  blog.save()  .then((result) => {    res.redirect('/blogs')      })  .catch((erro) => {    console.log(erro)  })

If, in any case, you want to use a text input to write the actions separated by space or comma, the actions property will be sent as a string. Then you can convert the string to array inside the controller.

// In case of multiple spaces, replace with single space. // Then split string to array of stringslet actions = req.body.actions.replace(/ +/g, " ").split(" ")const blog = new Blog ({    title: req.body.title,    actions});// rest of code

Whatever you choose to do, the actions will be stored as array of strings in the database.


Possibly you are sending an array as a string to the mongo. You can check this by checking the type of req.body.tags like so:

console.log(typeof req.body.tags)

If this returns a String, make sure you send the content as JSON to the database.