我使用的是jensegers-mongodb包。
这是我的收藏品
DB::connection('mongodb')->collection('users')->insertGetId(array('name' => 'John Doe', 'users' => [
[
'id' => 100,
'wallet'=>20.20
],
[
'id' => 101,
'wallet'=>10.66
]
]));
我正在尝试删除特定的wallet
记录。以下是代码
DB::connection('mongodb')->collection('users')->where('name', 'John Doe')->where('users.id',100)->unset('users.wallet');
但它并没有如预期的那样发挥作用。我怎样才能做到这一点?
要从子文档数组中取消设置字段,必须编写users.$.wallet
而不是users.wallet
DB::connection('mongodb')->collection('users')->where('name', 'John Doe')->where('users.id',100)->unset('users.$.wallet');
看看mongodb的官方unset文档。在行为章节中,将解释$
-符号。