Javascript 使用meteor时使用嵌套数组的$pull-in mongoDB
问题概述 创建用户时,预定义部门和相应类别(本质上是子部门)的列表将与其帐户关联 此部门和类别列表显示在用户/类别页面上(按部门分组) 然后,用户可以根据需要添加/编辑/删除部门和添加/删除类别 我的问题是,我不确定最初构造数据的最佳方法。然后,无论我用哪种方式,我都会在mongoDB查询中遇到麻烦 问题详细信息 我尝试在调用onCreateUser时以2种方式添加预定义的部门/类别数据,它们成功地添加到mongoDB (我还研究了将信息放入用户帐户和user.profile的顶层)Javascript 使用meteor时使用嵌套数组的$pull-in mongoDB,javascript,arrays,mongodb,meteor,Javascript,Arrays,Mongodb,Meteor,问题概述 创建用户时,预定义部门和相应类别(本质上是子部门)的列表将与其帐户关联 此部门和类别列表显示在用户/类别页面上(按部门分组) 然后,用户可以根据需要添加/编辑/删除部门和添加/删除类别 我的问题是,我不确定最初构造数据的最佳方法。然后,无论我用哪种方式,我都会在mongoDB查询中遇到麻烦 问题详细信息 我尝试在调用onCreateUser时以2种方式添加预定义的部门/类别数据,它们成功地添加到mongoDB (我还研究了将信息放入用户帐户和user.profile的顶层) 单独收藏
Categories.insert({
"user": userId,
"categories": [
{
"department": "Kitchen",
"items": [
"meat",
"dairy",
"fish",
"dry goods",
"ACME supplies"
]
},
{
"department": "Bar",
"items": [
"whiskey",
"red wine",
"white wine",
"champagne & sparkling"
]
}
]
});
Categories.insert({
"user": "userId",
"categories": [
{
"department": "Kitchen",
"item": "meat"
},
{
"department": "Kitchen",
"item": "dairy"
},
{
"department": "Kitchen",
"item": "fish"
},
{
"department": "Kitchen",
"item": "dry goods"
},
{
"department": "Bar",
"item": "whiskey"
},
{
"department": "Bar",
"item": "red wine"
},
{
"department": "Bar",
"item": "white wine"
},
{
"department": "Bar",
"item": "champagne & sparkling"
}
]
});
XXXXXXXXXX
Categories.insert({
"user": "userId",
"categories": [
{
"department": "Kitchen",
"item": "meat"
},
{
"department": "Kitchen",
"item": "dairy"
},
{
"department": "Kitchen",
"item": "fish"
},
{
"department": "Kitchen",
"item": "dry goods"
},
{
"department": "Bar",
"item": "whiskey"
},
{
"department": "Bar",
"item": "red wine"
},
{
"department": "Bar",
"item": "white wine"
},
{
"department": "Bar",
"item": "champagne & sparkling"
}
]
});
使用1。在上面和下面的助手中,我可以对数据进行迭代,并按部门对它们进行分组
`Template.categoriesPage.helpers({
getCategories: function(){
var userId = Meteor.userId();
var categoriesList = Categories.findOne({
user: userId
});
console.log(categoriesList);
var categories = categoriesList && categoriesList.categories;
return categories;
}
});`
以下是模板:
<template name="categoriesPage">
<div class="ui dividing header">
<h3 class="ui header">My Categories</h3>
</div>
<div class="ui grid">
{{#each getCategories}}
<div class="five wide column">
<table class="ui single line table">
<thead>
<tr>
<th>{{department}}</th>
<th class="right aligned"><span style="color:black" class="label-important label removeDepartment"><i class="remove icon large red"></i></span></th>
</tr>
</thead>
<tbody>
{{#each items}}
<tr>
<td>{{this}}</td>
<td class="right aligned"><span style="color:black" class="label-important label removeCategory"><i class="remove icon red"></i></span></td>
</tr>
{{/each}}
</tbody>
<tfoot>
<tr>
<th></th>
<th>Add Cat.</th>
</tr>
</tfoot>
</table>
</div>
{{/each}}
</div>
<form class="ui form">
<input type="submit" value="Submit" class="ui teal button submit"/>
</form>
</template>
以下是我在服务器上的方法:
`removeDepartment: function(userId, department) {
check(userId, String);
check(department, String);
Categories.update({user:userId},{ $pull: { categories: { department:department}}});
console.log('successfully deleted');
},
removeCategory: function(userId, item) {
check(userId, String);
check(item, String);
Categories.update({user:userId, "categories.department":department},{ $pull: { "categories.$.items":item}});
console.log('successfully deleted');
}`
这样的模式会起作用吗
`var kitchenId = Categories.insert({user: userId, department:"kitchen"});
Categories.insert({item:"dairy",parent:kitchenId});
Categories.insert({item:"dry goods",parent:kitchenId});
Categories.insert({item:"meat",parent:kitchenId});
Categories.insert({item:"fish",parent:kitchenId});
var barId = Categories.insert({user: userId, department:"bar"});
Categories.insert({item:"vodka",parent:barId});
Categories.insert({item:"whiskey",parent:barId});
Categories.insert({item:"white wine",parent:barId});
Categories.insert({item:"red wine",parent:barId});`
如对最佳实践有任何建议,将不胜感激。请记住,我将需要能够编辑部门和添加部门和类别。我还需要将部门和类别输入到动态下拉列表中
提前感谢你的帮助