Mongoose - Save an array of strings

I cannot save an array of strings in my DB using Mongoose .

(Note that all of the code below is simplified to write here)

So, I declare the face schema variable that I have:

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

The circuit itself looks like this:

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

And when it comes to keeping it simple:

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

Thus, using Postman, I send the array in the body - however, every time I check the database, it just shows one record with the whole array, i.e. how i sent it:

enter image description here

Any idea what else I should do?

+13
source share
7 answers

Record from my comment:

The way to specify an array of strings in a mongoose is as follows:

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

However, the problem here is most likely for Postman, since it sends the "array" as a string. You can verify this by checking the req.body.tags type as follows:

 console.log(typeof req.body.tags) 

If this returns a string, make sure that the content type in Postman for JSON is shown as this , and not by default, form-data '.

+38
source

Try changing the circuit to

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

or you can use mixed type

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

EDIT

I think the problem is with the appointment. Using:

 person.tags.push("string to push"); 
+3
source
 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); 
+2
source

First, as many people have pointed out, you need to change the scheme to indicate that the tags field is for storing an array of strings, not just one. To change this to:

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

Another thing you should keep in mind (and which caused me a lot of problems) is that when saving, be sure to use a fresh array for the tags field. For example, this one will not work:

 person.tags[0] = "new tag"; person.save(); 

Instead, you need to do something like:

 person.tags = person.tags.slice(); // Clone the tags array person.tags[0] = "new tag"; person.save(); 

Hope this helps.

+1
source
 var personSchema = new mongoose.Schema({ tags: [{type: String}] }); 

Use this in a circuit.

Saving an array:

 var etc = new modename({yourprimaryid: primaryid}); for (var i = 0; i < tag.length; i++) { etc.tag.push(tag[i]); } etc.save(function(err) { //whatever you want here } 
0
source

Define the scheme:

 const schema = new Schema({ name: { type: String, required: true }, tags: [String] }); 

In postman, add each element separately using the array syntax below

 name:Thing tags[]:task tags[]:other tags[]:thing 

Returned data:

 { "__v": 0, "name": "Thing", "_id": "5a96e8d0c7b7d1323c677b33", "tags": [ "task", "other", "thing" ] } 
0
source
  1. On the diagram

    techs: Array

  2. At the postman

    "techs": ["express","rect","html","css","scss"]

  3. On the database (MongoDB)

    "techs": [ "epxress", "rect", "html", "css", "scss" ]

0
source

Source: https://habr.com/ru/post/1243416/


All Articles