我有三个集合:用户,问题和答案。用户可以选择问题和答案作为收藏。我需要显示用户标记为收藏的问题和答案。
哪种方法最好?
我的第一个想法是在问题和答案中创建一个数组字段,这样可以保存喜欢该问题或答案的users_id。然后,为了获取信息,我应该进行两个查询:db.questions.find({favorites:user_id})和db.answers.find({favorites:user_is})。但是,我想在一个查询中提供信息。
我还想在用户集合中保存一个包含questions_ids和answers_ids的数组。但我不确定这是最好的方法,我也不知道如何使用Mongoose来定义它。
提前谢谢
答案 0 :(得分:1)
I would go with the latter approach. So on your user model, you'd have the following defined:
t(apply(marketReturns*(1-spend),1,cumprod))*initPortBal
With that defined in the model, your "mark as favorite" logic could be something like:
$("#create_node_group").submit(function() { // catch the form's submit
alert("submit called");
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
$('#content').html(response); // update the DIV
}
});
Then when you need the documents just call <?php
include "db.php";
$query = "select * from monitor_template";
$template = array();
$row_num;
$result=pg_query($conn, $query);
if (!$result) {
echo "query did not execute";
}
if (pg_num_rows($result) == 0) {
echo "0 records";
}
else {
$a=0;
while ($row = pg_fetch_array($result)) {
//do stuff with $row
$template[$a][0]=$row[mon_template_id];
$template[$a][1]=$row[template_name];
$a=$a+1;
}
}
$row_num=pg_num_rows($result); //row count starts from 0
//echo 'rows returned are: '.$row_num;
//print_r($template);
//echo $template[0][0].'-'.$template[0][1].'--'.$template[1][0].'-'.$template[1][1];
?>
<table>
<form class="content_form" id="create_node_group" name = "create_node_group" action="process_create_node_group.php" method="post">
<tr><td>
Node Group Path: </td> <td><input type="text" name="path"></td></tr>
<tr><td>
Node Group ID: </td><td><input type="text" name="node_group_id" disabled="disabled"></td></tr>
<tr><td>
Node Group Name:</td> <td> <input type="text" name="node_group_name"></td></tr>
<?php
for($i=1;$i<=5;$i++)
{
echo '<tr><td>';
echo 'Monitoring Template '. $i.':</td><td> <select id="Mon_template'.$i.'" name="Mon_template'.$i.'">';
echo ' <option value="">Select</option>';
for($j=0;$j<$row_num;$j++)
{
echo '<option value="'.$template[$j][0].'">'.$template[$j][1].'</option>';
}
echo '</select >';
echo '</td></tr>';
}
?>
<tr><td colspan="2">
<input type="submit" value="Submit" >
</td></tr>
</form>
</table>
: http://mongoosejs.com/docs/populate.html
{
favoriteAnswers: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Answer'
default: []
}],
favoriteQuestions: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Question'
default: []
}]
}