ThanhLam112358
ThanhLam112358

Reputation: 916

Find all and modify document in MongoDB

I have document in MongoDB like this

{"_id":{"$oid":"5eb8c55230cb8651e0906d7e"},"rolecode":"DHBK_ROLE_01","functioncode":"DHBK_FUNC_01, DHBK_FUNC_02","productid":"mBaaS_Platform","comid":"DHBK"}
{"_id":{"$oid":"5eb8c5a030cb8651e0906d7f"},"rolecode":"DHBK_ROLE_02","functioncode":"DHBK_FUNC_02, DHBK_FUNC_03","productid":"GIS_Platform","comid":"DHBK"}
{"_id":{"$oid":"5ebe0016b4146803d8ad2559"},"rolecode":"DHKT_ROLE_01","functioncode":"DHKT_FUNC_01, DHKT_FUNC_02","productid":"mBaaS_Platform","comid":"DHKT"}
{"_id":{"$oid":"5ebe003cb4146803d8ad255a"},"rolecode":"DHKT_ROLE_02","functioncode":"DHKT_FUNC_01","productid":"Analysis_Platform","comid":"DHKT"}
{"_id":{"$oid":"5ebe00ffb4146803d8ad255b"},"rolecode":"DHBK_ROLE_03","functioncode":"DHBK_FUNC_01, DHBK_FUNC_02,DHBK_FUNC_03","productid":"IOT_Platform","comid":"DHBK"}
{"_id":{"$oid":"5ecf89f969f07e1cb0ad063d"},"rolecode":"DHBK1_ROLE_01","functioncode":"DHBK1_FUNC_1,DHBK_FUNC_03","productid":"IOT_Platform","comid":"DHBK1"}
{"_id":{"$oid":"5ecf8b6169f07e1cb0ad063e"},"rolecode":"DHBK1_ROLE_02","functioncode":"DHBK1_FUNC_1,DHBK_FUNC_02","productid":"SSO_Platform","comid":"DHBK1"}
{"_id":{"$oid":"5ecf8b7969f07e1cb0ad063f"},"rolecode":"DHBK1_ROLE_03","functioncode":"DHBK1_FUNC_1","productid":"ABC_Platform","comid":"DHBK1"}

I want to find documents have DHBK1_FUNC_1 in functioncode and modify this property to ""

For example:

Step 1: Find documents have DHBK1_FUNC_1 in functioncode, we have:

{
    "rolecode": "DHBK1_ROLE_01",
    "functioncode": "DHBK1_FUNC_1,DHBK_FUNC_03",
    "productid": "IOT_Platform",
    "comid": "DHBK1"
}

{
    "rolecode": "DHBK1_ROLE_02",
    "functioncode": "DHBK1_FUNC_1,DHBK_FUNC_02",
    "productid": "SSO_Platform",
    "comid": "DHBK1"
}

{
    "rolecode": "DHBK1_ROLE_03",
    "functioncode": "DHBK1_FUNC_1",
    "productid": "ABC_Platform",
    "comid": "DHBK1"
}

Step 2: Modify, we have:

{
    "rolecode": "DHBK1_ROLE_01",
    "functioncode": ",DHBK_FUNC_03",
    "productid": "IOT_Platform",
    "comid": "DHBK1"
}

{
    "rolecode": "DHBK1_ROLE_02",
    "functioncode": ",DHBK_FUNC_02",
    "productid": "SSO_Platform",
    "comid": "DHBK1"
}

{
    "rolecode": "DHBK1_ROLE_03",
    "functioncode": "",
    "productid": "ABC_Platform",
    "comid": "DHBK1"
}

Thank you in advance

Upvotes: 0

Views: 83

Answers (1)

Vaibhav Bharati
Vaibhav Bharati

Reputation: 34

Syntax:

db.collection.updateMany({filter},{update})

Solution:

    Replace collection_name with your collection.

db.collection_name.updateMany({"functioncode": "DHBK1_FUNC_1",},{'$set':{"functioncode":""},})

Upvotes: 1

Related Questions