Home > Net >  MongoDB $addToSet to deep nested array of object
MongoDB $addToSet to deep nested array of object

Time:09-10

Below is my data structure.

{
    "_id" : "room1",
    "members" : [ 
        {
            "_id" : "member1",
            "name" : "Michael",
            "payments" : [
                {
                    "month": "2018/09"
                    "amount": "20"
                }
            ]
        }, 
    ]
}

I want to push below object to Michael's payments

{ 
  "month": "2018/09", 
  "amount": "5000" 
}

In this case, What I want to is overwrite object, because month: "2018/09" already exist. Like below :

{
    "_id" : "room1",
    "members" : [ 
        {
            "_id" : "member1",
            "name" : "Michale",
            "payments" : [
                {
                    "month": "2018/09"
                    "amount": "5000"
                }
            ]
        }, 
    ]
}

And, In case when I want to push object that not exist same month in payments, I want to add this object to payments.

{ 
  "month": "2018/10", 
  "amount": "2000" 
}

So the expected result is

{
    "_id" : "room1",
    "members" : [ 
        {
            "_id" : "member1",
            "payments" : [
                {
                    "month": "2018/09"
                    "amount": "5000"
                },
                {
                    "month": "2018/10"
                    "amount": "2000"
                }
            ]
        }, 
    ]
}

I tried like below, but it's not working. My code generate duplicated new month object every time I tried. How can I do this properly?

Rooms.update(
    {
        _id: "room1",
        "members._id": "member1",
        "members.$.payments": {
            $not: {
                $elemMatch: {
                    month: req.body.month
                }
            }
        }
    },
    {
        $addToSet: {
            "members.$.payments": {
                month: req.body.month,
                amount: req.body.value
            }
        }
    },
    { multi: true }, function (err, result) {
        console.log(result)
    }
)

CodePudding user response:

You can use below command to add without duplicity either in months or amount

Rooms.update(
    {
        _id: "room1",
        "members._id": "member1"
    },
    {
        $addToSet: {
            "members.$.payments": {
                month: req.body.month,
                amount: req.body.value
            }
        }
    },function (err, result) {
        console.log(result)
    }
)

CodePudding user response:

So I heard I have to determine duplication myself, so below is my code... it's writing now.,,


So Finally this is my code

  Clubs.findOne({ 
        uid: req.params.club_id,
        "members._id": mongoose.Types.ObjectId(req.params.member_uid)
    }, function(err, club){

        let member = club.members.filter(el => {
            if(el._id.equals(req.params.member_uid)) return el
        })

        let duplicated = false; 
        member[0].payments.map(el => {
            if(el.month === req.body.month) duplicated = true
        })

       if(duplicated){

        Clubs.update(
            {
                uid: req.params.club_id,
                "members._id": mongoose.Types.ObjectId(req.params.member_uid),
            },
            {
                $set: {
                    ["members.$.payments." index ".amount"] : req.body.value
                }
            },
            function (err, result, third) {
                if (err) throw err
                console.log('result')
                console.log(result)
                res.json({})
            }
        )


    } else {

        Clubs.update(
            {
                uid: req.params.club_id,
                "members._id": mongoose.Types.ObjectId(req.params.member_uid),
            },
            {
                $push: {
                    "members.$.payments" : { 
                        month : req.body.month, 
                        amount: req.body.value 
                    }
                }
            },
            function (err, result, third) {
                if (err) throw err
                console.log('result')
                console.log(result)
                res.json({})
            }
        )
    }

    })

CodePudding user response:

Perhaps consider changing the structure of your nested array to an object? So change this

{
  "payments": [{
      "month": "2018/09"
      "amount": "5000"
    },
    {
      "month": "2018/10"
      "amount": "2000"
    }
  ]
}

to this:

{
  "payments": {
    "2018/09": "5000",
    "2018/10": "2000"
  }
}

Then you can do a simple update:

Rooms.update({
    _id: "room1",
    "members._id": "member1",
    "members.payments": {
      $exists: true
    }
  }, {
    $set: {
      "members.payments."   req.body.month: req.body.value
    }
  },

)
  • Related