Search code examples
mongodbmongodb-queryaggregation-frameworkmongoidcriteria

How to insert and update value in mongodb?


    {_id :1,    
    name : ABC,
address :India,
    ph : { 9856235412, 23654122 }
}

1. How to add one more value of Ph?

means I have to add value 02152123, result should have

{_id :1,    
    name : ABC,
address :India,
    ph : { 9856235412, 23654122, 02152123 }
}

2. How to update value 9856235412 of Ph?

, result should have

{_id :1,    
    name : ABC,
address :India,
    ph : { 888212225, 23654122, 02152123 }
}

Please help me criteria in java, i beginer for mongo db


Solution

  • I'm assuming ph is actually an array and not an object structure, so the data structure is:

    {_id :1, name : ABC, address :India, ph : [ 9856235412, 23654122 ] }

    This should do it:

    db.collection.update({$push:{ph : 02152123}}, {_id:1})