MongoDB $addToSet 到对象的深层嵌套数组

ton*_*on1 6 mongoose mongodb node.js mongodb-query

下面是我的数据结构。

{
    "_id" : "room1",
    "members" : [ 
        {
            "_id" : "member1",
            "name" : "Michael",
            "payments" : [
                {
                    "month": "2018/09"
                    "amount": "20"
                }
            ]
        }, 
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想将下面的对象推到迈克尔的payments

{ 
  "month": "2018/09", 
  "amount": "5000" 
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我想要的是覆盖对象,因为month: "2018/09"已经存在。像下面这样:

{
    "_id" : "room1",
    "members" : [ 
        {
            "_id" : "member1",
            "name" : "Michale",
            "payments" : [
                {
                    "month": "2018/09"
                    "amount": "5000"
                }
            ]
        }, 
    ]
}
Run Code Online (Sandbox Code Playgroud)

而且,如果我想推送month付款中不存在的对象,我想将此对象添加到payments.

{ 
  "month": "2018/10", 
  "amount": "2000" 
}
Run Code Online (Sandbox Code Playgroud)

所以预期的结果是

{
    "_id" : "room1",
    "members" : [ 
        {
            "_id" : "member1",
            "payments" : [
                {
                    "month": "2018/09"
                    "amount": "5000"
                },
                {
                    "month": "2018/10"
                    "amount": "2000"
                }
            ]
        }, 
    ]
}
Run Code Online (Sandbox Code Playgroud)

我尝试如下,但它不起作用。month每次我尝试时,我的代码都会生成重复的新对象。我怎样才能正确地做到这一点?

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)
    }
)
Run Code Online (Sandbox Code Playgroud)

ton*_*on1 1

所以我听说我必须自己确定重复,所以下面是我的代码......现在正在写。,,


所以最后这是我的代码

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

    })
Run Code Online (Sandbox Code Playgroud)