我在mongodb中有一个这样的集合。
"_id" : ObjectId("554c5397ccfff21e103c9869"),
"name" : "test",
"color" : [
"552ced22ccfff2d8183c986a_Jellow",
"551fdd24ccfff2362e3c9869_test"
],
"updated_at" : ISODate("2015-05-08T06:11:35.303Z"),
"created_at" : ISODate("2015-05-08T06:11:35.303Z")
我只想更新数组颜色中的一个值,但是当我尝试更新数组时,它将删除颜色数组中的所有值,并将其替换为新值。这是代码。(我正在使用JESSENGER MONGODB包装用于LARAVEL)
$query->where($field,'regexp','/^('.$id.')_.*/')->update([$field=>$id.'_'.$name]);
我应该怎么做。?
您想要做的是,将架构更改为{key:value}对,然后按照本教程进行操作,这将帮助您解决问题。或者,您可以从颜色数组中获取所有值并替换为新值,然后更新您的文档(我不会这样做,因为这是一种肮脏的方法!)。
编辑
嘿芽!我在詹森格(Jenssenger)文档上创建了此文件:
将项目添加到数组。
DB::collection('users')->where('name', 'John')->push('items', 'boots');
DB::collection('users')->where('name', 'John')->push('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));
如果您不想重复项目,请将第三个参数设置为true:
DB::collection('users')->where('name', 'John')->push('items', 'boots', true);
从数组中删除一项。
DB::collection('users')->where('name', 'John')->pull('items', 'boots');
DB::collection('users')->where('name', 'John')->pull('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句