我有一个带有两个集合的数据库。学生集合有一个名为“student_bookselections”的字段,用于保存书籍的ID。该字段的内容通常会被更改或更新。
学生集合
array(
"student_id" => 4,
"student_fullname" => $studentfullname,
"student_bookselections" => 'id1, id34, id788 ... ... id4'
)
书籍收藏
array(
"book_id" => 4354,
"book_title" => $title,
"book_author" => $author,
)
答案 0 :(得分:1)
你最好有第三张桌子将学生与书籍联系起来,你当前的实施没有规范化。
Users(
"student_id" => 4,
"student_fullname" => $studentfullname
)
Books(
"book_id" => 4,
"book_title" => $title,
"book_author" => $author
)
Selections(
"selection_id" => 4,
"student_id" => $studentid,
"book_id" => $bookid
)
然后,您可以定义与外键的关系以链接表,更重要的是,每行与一条信息相关。如果您想知道某个学生的所有书籍选择
mysql_query("SELECT * FROM Selections WHERE student_id = $student_id");
希望有所帮助!
答案 1 :(得分:0)
更新
<?php
$mongo = new Mongo();
$db = $mongo->selectDB("Your_Database");
$student = $db->students;
$update = array("$set" => array("student_bookselections" => "your ids"));
$where = array("student_id" => 4);
$student->update($where,$update);
?>
要插入
<?php
$mongo = new Mongo();
$db = $mongo->selectDB("Your_Database");
$student = $db->students;
$insert = array("student_fullname" => "Stefan", "student_bookselections" => "your ids" );
$student->insert($insert);
?>
要删除
<?php
$mongo = new Mongo();
$db = $mongo->selectDB("Your_Database");
$student = $db->students;
$student->remove(array("ID" => 4));
?>
<强>关系强> (见Crhis的帖子)
Users(
"student_id" => 4,
"student_fullname" => $studentfullname
)
Books(
"book_id" => 4,
"book_title" => $title,
"book_author" => $author
)
Selections(
"selection_id" => 4,
"student_id" => $studentid,
"book_id" => $bookid
)
选择此关系
<?php
$mongo = new Mongo();
$db = $mongo->selectDB("Your_Database");
$selection = $db->selections;
$res = $student->find(array("student_id" => $student_id));
?>