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'
}]});
我想更新特定员工的项目状态。 例如,我想更新第1项,为 EmployeId =“1”设置 EmployName =“Patrick Power”,因为该员工存在:
db.students.update(
{'_id':'shop1','Employee.EmployId':'1'},
{$set:{'Employee.$.EmployName':'Patrick Power'} }
);
这是我一直试图做的但是不起作用?
我做错了什么?
答案 0 :(得分:1)
这可能对您有所帮助:
db.collectionName.update({
"_id": "shop1",
'Employee': {
'$elemMatch': {
'EmployId': '1'
}
}
}, {
$set: {
'Employee.$.EmployName': 'Patrick Power'
}
});