Append a string to the end of an existing field in MongoDB

后端 未结 6 1291
灰色年华
灰色年华 2020-12-02 23:22

I have a document with a field containing a very long string. I need to concatenate another string to the end of the string already contained in the field.

The way I

6条回答
  •  不思量自难忘°
    2020-12-03 00:12

    this is a sample of one document i have :

    {
        "_id" : 1,
        "s" : 1,
        "ser" : 2,
        "p" : "9919871172",
        "d" : ISODate("2018-05-30T05:00:38.057Z"),
        "per" : "10"
    }
    

    to append a string to any feild you can run a forEach loop throught all documents and then update desired field:

    db.getCollection('jafar').find({}).forEach(function(el){
        db.getCollection('jafar').update(
            {p:el.p},
            {$set:{p:'98'+el.p}})    
        })
    

提交回复
热议问题