0
db.employee.insert(
{
_id: 'shop1',
ShopName: 'Londis',
ShopAddress: 'Clea boy',
Owner: 'Tim Byrne',
ShopContactNumber: '0877733121',
Employee: [
            {
            EmployId: '1',
            EmployName: 'Pat Power',
            EmployContactNumber: 0876395224,
            EmployAddress: 'Lacka Rd',
            Salary: 500.00,
            Hours: 40,
            PayType: 'Cheque',
            Wage: 9.00,
            EmployeeType: 'Manager'
            },
            {
            EmployId: '2',
            EmployName: 'Craig Coad',
            EmployContactNumber: 0873347582,
            EmployAddress: 'Portlaw',
            Salary: 400.00,
            Hours: 32,
            PayType: 'Bank',
            Wage: 8.65,
            EmployeeType: 'FloorStaff'
            },
            {
            EmployId: '3',
            EmployName: 'Joe Bloggs',
            EmployContactNumber: 0861234567,
            EmployAddress: 'Waterford',
            Salary: 510.00,
            Hours: 12,
            PayType: 'Cheque',
            Wage: 9.50,
            EmployeeType: 'Manager'
            }]});

I want to update the state of an item for a specific Employee. For example, I would want to update item 1 to set EmployName = "Patrick Power" for EmployeId = "1" because that employee exists:

db.students.update(
    {'_id':'shop1','Employee.EmployId':'1'},
    {$set:{'Employee.$.EmployName':'Patrick Power'} }
);

This is what i have been trying to do but it dose not work?

What am i doing wrong ?

Community
  • 1
  • 1
  • 1
    possible duplicate of [How can i update the sub document field from the sub dcoument array based on the query?](http://stackoverflow.com/questions/29667369/how-can-i-update-the-sub-document-field-from-the-sub-dcoument-array-based-on-the) – Neo-coder Apr 17 '15 at 09:32

1 Answers1

1

This might be helpful to you :

db.collectionName.update({
"_id": "shop1",
'Employee': {
    '$elemMatch': {
        'EmployId': '1'
    }
}
}, {
$set: {
    'Employee.$.EmployName': 'Patrick Power'
}
});
Vishwas
  • 6,967
  • 5
  • 42
  • 69