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

时间:2018-09-12 11:55:44

标签: node.js mongodb mongoose mongodb-query

下面是我的数据结构。

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

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

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

在这种情况下,我要覆盖的对象是因为month: "2018/09"已经存在。如下所示:

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

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

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

所以预期结果是

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

我尝试过如下操作,但是没有用。每次尝试时,我的代码都会生成重复的新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)
    }
)

2 个答案:

答案 0 :(得分:0)

您可以使用以下命令以不重复的形式添加月份或金额

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

答案 1 :(得分:0)

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


所以最后这是我的代码

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

    })