我在这个集合中有这样的集合我需要从数组中提取或删除详细信息,其中登录日期= 2015-12-23。数组的重复应该是相同的
Array
(
[_id] => 1
[user_id] => 2
[work_details] => Array
(
[0] => Array
(
[login_date] => 2015-12-23
[work_history] => Array
(
[0] => Array
(
[details] => gsdgsdgd
[datetime] => 2015-12-23 12:54:12
)
)
)
[1] => Array
(
[login_date] => 2015-12-24
[work_history] => Array
(
[0] => Array
(
[details] => ffgdf dfgdfh
[datetime] => 2015-12-24 12:54:12
)
)
)
)
)
我试过这样的
$this->mongo_db->where('login_date', "2015-12-23")->unset_field('login_date.$')->update('masterCollection');
$this->mongo_db->pull('login_date', NULL)->update('masterCollection');
但它不起作用
删除记录后,我需要像这样的数组
Array
(
[_id] => 1
[user_id] => 2
[work_details] => Array
(
[0] => Array
(
[login_date] => 2015-12-24
[work_history] => Array
(
[0] => Array
(
[details] => ffgdf dfgdfh
[datetime] => 2015-12-24 12:54:12
)
)
)
)
)
有没有解决方案。
谢谢
答案 0 :(得分:1)
以下是我用来删除数据的查询:
db.testso.update({_id: 2},{ $pull: { work_details: {login_date:"2015-12-25"}}});
使用login_date成功删除了数组:“2015-12-25”。
以下是我使用的示例数据:
{ "_id" : 1, "user_id" : 2, "work_details" : [ { "login_date" : "2015-12-23", "work_history" : [ { "details" : "abcd", "day" : "ancxc" } ] } ] }
{ "_id" : 2, "user_id" : 3, "work_details" : [ { "login_date" : "2015-12-24", "work_history" : [ { "details" : "abcd1", "day" : "ancxc1" } ] }, { "login_date" : "2015-12-25", "work_history" : [ { "details" : "abcd2", "day" : "ancxc2" } ] } ] }