Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose - Save array of strings

I can't save an array of strings into my DB using Mongoose.

(Note all code below is simplified for ease of writing here)

So i declare a variable of a person schema I have:

var newPerson = new Person ({     tags: req.body.tags }); 

The schema itself looks like:

var personSchema = new mongoose.Schema({   tags: Array }); 

And when it comes to saving its just a simple:

newPerson.save(function(err) {     //basic return of json }); 

So using Postman I send in an array in the body - however everytime I check the DB, it just shows one entry with the array as a whole i.e. how I sent it:

enter image description here

Any ideas what extra I'm supposed to do?

like image 797
userMod2 Avatar asked Feb 19 '16 16:02

userMod2


People also ask

Can you store array in MongoDB?

One of the benefits of MongoDB's rich schema model is the ability to store arrays as document field values. Storing arrays as field values allows you to model one-to-many or many-to-many relationships in a single document, instead of across separate collections as you might in a relational database.

What is ObjectId in Mongoose?

ObjectId . A SchemaType is just a configuration object for Mongoose. An instance of the mongoose. ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a configuration for a path in a schema.

What is Save () in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

What does save () return in Mongoose?

The save() method is asynchronous, and according to the docs, it returns undefined if used with callback or a Promise otherwise.


2 Answers

Write up from my comment:

The way to specify an array of strings in mongoose is like so:

var personSchema = new mongoose.Schema({ tags: [{     type: String }] 

However, the problem here is most-likely to do with Postman as it is sending the 'array' as a string. 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 to set the content-type in Postman to JSON as seen in this screenshot rather than the default 'form-data' option.

like image 114
Ash Avatar answered Sep 28 '22 16:09

Ash


var schema = new Schema({   name:    String,   binary:  Buffer,   living:  Boolean,   updated: { type: Date, default: Date.now },   age:     { type: Number, min: 18, max: 65 },   mixed:   Schema.Types.Mixed,   _someId: Schema.Types.ObjectId,   decimal: Schema.Types.Decimal128,   array: [],   ofString: [String],   ofNumber: [Number],   ofDates: [Date],   ofBuffer: [Buffer],   ofBoolean: [Boolean],   ofMixed: [Schema.Types.Mixed],   ofObjectId: [Schema.Types.ObjectId],   ofArrays: [[]],   ofArrayOfNumbers: [[Number]],   nested: {     stuff: { type: String, lowercase: true, trim: true }   },   map: Map,   mapOfString: {     type: Map,     of: String   } })  // example use  var Thing = mongoose.model('Thing', schema);  var m = new Thing; m.name = 'Statue of Liberty'; m.age = 125; m.updated = new Date; m.binary = Buffer.alloc(0); m.living = false; m.mixed = { any: { thing: 'i want' } }; m.markModified('mixed'); m._someId = new mongoose.Types.ObjectId; m.array.push(1); m.ofString.push("strings!"); m.ofNumber.unshift(1,2,3,4); m.ofDates.addToSet(new Date); m.ofBuffer.pop(); m.ofMixed = [1, [], 'three', { four: 5 }]; m.nested.stuff = 'good'; m.map = new Map([['key', 'value']]); m.save(callback); 
like image 45
Phonix Avatar answered Sep 28 '22 17:09

Phonix