update(array("loc1"=>$locations[0]),array('$set'=>(object)array("Users"=>(array("user_id"=>$user_id,"user_name"=>$Email)))));
我编写了上面的代码将user_id插入到数组用户中,执行后我得到mongodb中的结果如下所示
{
"_id" : ObjectId("5702cfd2c693b54008000035"),
"loc1" : "Anapara",
"loc2" : "Puthucurichy",
"loc3" : "Kadinamkulam",
"loc4" : "Kerala",
"loc5" : "India",
"Users" : {
"user_id" : ObjectId("5702cfd2c693b54008000034"),
"user_name" : "chunks@yahoo.com"
}
}
这里关键字用户不会成为一个数组,我的意思是我希望它采用这种格式(方括号中的用户值):
"Users" :[ {
"user_id" : ObjectId("5702cfd2c693b54008000034"),
"user_name" : "chunks@yahoo.com"}
]
我应该在php代码中做什么修改才能将值放在方括号中?
答案 0 :(得分:0)
BSON数组([ ... ]
)。所以你的代码需要(我会允许自己使其更具可读性以便更好地说明):
update(
array("loc1" => $locations[0]),
array('$set' => array(
"Users"=> array( // this makes Users a list
array("user_id"=>$user_id,"user_name"=>$Email) // this is your embedded object
// here could go another embedded object separated by a comma
)
))
);