1

I have a MongoDB document like this:

{
  "_id": ObjectId("5589044a7019e802d3e9dbc5"),
  groups:[
    {
      _id: 5a3326d01b2213894672fdd0,
      group_id: '13239517',
      thread_id: 'f32940da-12-15-01-9325790',
      Last_post_id: '981206845'
    }
  ]
}

I've tried a variation of $set and $unset using:

User.findOneAndUpdate({userId}, {groups:{$set: {thread_id: ''}}}, {new: true})

when I tried multiple variations of the above code I keep on getting groups where the affected object is only left with an id.

[
  { _id: 5a3326d41b2213894672fdd1 },
  {
    _id: 5a332a81479a728ebe4226e5,
    group_id: '13239517',
    thread_id: 'f32940da-12-15-01-9325790',
    Last_post_id: '981206845'
  }
]

I've also tried looping over the array of groups removing the key using reduce then just trying to set the groups to this new values like and that didn't work either.

let groups = groups.reduce((prev, ele)=>{
  // group_id is passed in as an argument
  if(ele.group_id === group_id){
    let chatThread = ele.thread_id
    delete ele.thread_id
    console.log(ele)
    return chatThread
  } else {
    return prev
  }
}, false)
User.findOneAndUpdate({userId}, {groups}, {new: true})

this just doesn't do anything.

My intended result would be where thread_id is gone

groups:[
  {
    _id: 5a3326d01b2213894672fdd0,
    group_id: '13239517',
    Last_post_id: '981206845'
  }
]

Although similar to link1 and link2 both of those solutions either don't change anything when saved or remove all properties other than _id.

Joshua Fermin
  • 647
  • 9
  • 19
  • Possible duplicate of [Remove a field from array element in mongodb](https://stackoverflow.com/questions/19945924/remove-a-field-from-array-element-in-mongodb) – s7vr Dec 15 '17 at 02:43
  • As this is a duplicate, have a look at: https://stackoverflow.com/questions/19945924/remove-a-field-from-array-element-in-mongodb – rieckpil Dec 15 '17 at 04:23
  • @Veeram I tried those and it did not work for me it would either remove all properties except _id or do nothing at all. I did find a solution though based on those links so thank you very much. User.findOne({userId}, async(err, user)=>{ let newGroups = user.groups.map(ele=>{ if(ele.group_id === group_id){ return {group_id:ele.group_id,_id: ele._id,Last_post_id: ele.Last_post_id} }return ele })var updateUserGroups = await User.findOneAndUpdate({userId}, {groups: newGroups}, {new: true}) }) – Joshua Fermin Dec 15 '17 at 15:53

1 Answers1

0

this did work for me. looks like reduce and map when using delete keyword will not affect the original array I assumed it would since i thought it was passed by reference.

User.findOne({userId}, async(err, user)=>{
            let new_groups = user.groups.map(ele=>{
              // group id is passed as an argument
              if(ele.group_id === group_id){
                return {
                  group_id:ele.group_id,
                  _id: ele._id,
                  Last_post_id: ele.Last_post_id
                }
              }
              return ele
            })
            var updateUserGroups = await User.findOneAndUpdate({userId}, {groups: new_groups}, {new: true})
Joshua Fermin
  • 647
  • 9
  • 19
  • You can use `User.update( {userId}, {$unset: {"groups.$[].thread_id":""}} )` in 3.6 to remove all the thread_ids from group array. – s7vr Dec 15 '17 at 16:04