Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB $addToSet to deep nested array of object

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)
    }
)
like image 923
ton1 Avatar asked Oct 16 '22 13:10

ton1


2 Answers

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)
    }
)
like image 168
Sagar Pandey Avatar answered Oct 23 '22 04:10

Sagar Pandey


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({})
            }
        )
    }

    })
like image 21
ton1 Avatar answered Oct 23 '22 06:10

ton1