1

picture of the problem:

enter image description here

I install the csv file without any problem, but it sees array as a string. The category must be an array.

sample code

  {  "_id":{"$oid":"609196655b92b7164b2cb016"},
     "body":"test",
     "category":"["Aksiyon Oyunları","Beceri Oyunları"]",
     "date":"1617272388425",
      "gamelink":"ssdsd",
      "imglink":"deneme görseli",
      "status":"published", 
      "title":"BUNU YAPIN TREN!",
      "slug":"bunu-yapin-tren!" }
1

1 Answer 1

0
    const postSchema = new Schema({
        title: {
            type: String,
            required: true
        },
        category: {
            type: [String],
            required: true
        }
    });

module.exports = new model('posts', postSchema);

schema and this code with node js

mongoose
    .connect(MONGO_URL)
    .then(() => console.log('DB Connected'))
    .catch(err => console.log("Fail to connect mongodb ", err));

    var ops = [];
    post_schema.find({ "category": { "$type": 2} }, function(err, arr) {
        arr.forEach(doc => {
            let str = doc.category+``
            str = str.split(',');
            const arr = str.map(e=>e.replace(/"|\[|\]|\\/gm,'').toString())
            ops.push({
              "updateOne": {
                "filter": { "_id": doc._id },
                "update": { "$set": { "category": arr } }
              }
            });
            if ( ops.length > 0 ) {
                console.log("BULKWRITE")
              post_schema.bulkWrite(ops);
              ops = [];
            }      
          }); 
    })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.