Aravindh
Aravindh

Reputation: 451

delete array field in a mongodb database

I am using mongoDB and my JSON (table name is "student") looks like

           [{
             name : "John",
             subjects:["English", "Maths"]
            },
            {
             name : "Winsel",
             subjects : ["Maths"]
            }]

here "subjects" is some other table and associated with student, I able to get data by using find query,

student.find({}).populate('subjects').then(function(studentData){console.log(studentData)})

by using this query i can get Data as

           [{
             name : "John",
             subjects:["English", "Maths"]
            },
            {
             name : "Winsel",
             subjects : ["Maths"]
            }]

after that i need to delete the field "subjects" which length is less than 2, I am using

             if(studentData.subjects.length < 2){
                 delete studentData.subjects
             }

but it doesn't delete the field, it shows the same result when i not using the if condition. my expected JSON is

      [{
         name : "John",
         subjects:["English", "Maths"]
        },
        {
         name : "Winsel"
        }]

Can any one guide me, Thanks in advance

Upvotes: 2

Views: 117

Answers (5)

Anandapriyan S.D
Anandapriyan S.D

Reputation: 315

Try this

var studentsList = [{
                      name : "John",
                      subjects:["English", "Maths"]
                    },
                    {
                      name : "Winsel",
                      subjects : ["Maths"]
                   }];

var newStudentList = _.map(studentsList , function(eachStudentData){
                          if(eachStudentData.subjects <2){
                              return _.pick(eachStudentData, 'name');
                          } else {
                              return eachStudentData;
                          }
                     });

You can use the newly formed students list newStudentList for your code

Upvotes: 1

Shubham
Shubham

Reputation: 1426

I don't know what are you trying to achieve here but populate works on application level . you can directly use aggregation pipeline for this using $project(or similar $addFields) and $lookup

db.students.aggregate(

    [
        {
            $addFields: {
               subjects:{
                 $cond:[{$gte:[{$size:"$subjects"},2]},"$subjects",null]
               }
            }
        },
        {
            $lookup: {
                "from" : "subjects",
                "localField" : "subjects",
                "foreignField" : "name",
                "as" : "subjects"
            }
        },

    ]
);

from mongodb 3.6 you can use "$$REMOVE" instead of null in addFields stage and also after $lookup you can use $project again to remove empty array

Upvotes: 0

edkeveked
edkeveked

Reputation: 18401

To remove the fields in your json response, you can use delete inside the callback function of your query function.

student.find({}).populate('subjects').then(function(studentData){
      var newData = studentData;

      newData.map(item => {
        if(item.subjects.length < 2) {
            delete item.subjects;
        }
        return item;
      })
      // you can use the newData here;

      })

Upvotes: 1

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7171

try this code

var data = [{
    name: "John",
    subjects: ["English", "Maths"]
  },
  {
    name: "Winsel",
    subjects: ["Maths"]
  }
];
console.log(data);
for (var i = 0; i < data.length; i++) {
  if (data[i].subjects.length < 2) {
    delete data[i].subjects;
  }
}
console.log(data);

Upvotes: 0

David Vicente
David Vicente

Reputation: 3131

This works to me. I don't know if you are working with a JSON object and you need to transform it to javascript object (students.toObject()).

var students = studentsJSON.toObject();

var newStudents = students.map(studentData => {
    if(studentData.subjects.length < 2) {
        delete studentData.subjects;
    }
    return studentData;
});

Upvotes: 0

Related Questions