gpt4 book ai didi

mongodb - 用 meteor 填充在mongodb中

转载 作者:可可西里 更新时间:2023-11-01 09:12:17 25 4
gpt4 key购买 nike

我正在使用 meteor.js。我有三个收藏板、类别、用户。

Boards : 
{
"_id": ObjectId("su873u498i0900909sd"),
"locked": NumberInt(0),
"board_name": "Legends",
"description": "legends of the world",
"cat_id": ObjectId("su873u498i0900909sd"),
"cost": NumberInt(1),
"image": "1389249002691_indexhj.jpeg",
"creator": ObjectId("52ce317994acdcee0fadc90c")
}

categories:
{
"_id": ObjectId("su873u498i0900909sd"),
"user_id": ObjectId("su873u498i0900909sd"),
catname:"hjjj"
}

users:
{
"_id":ObjectId(55acec2687fe55f12ded5b4a),
"username" :"mariya"
}

由此我想在用户集合中获取用户名,该用户名在类别集合中使用 user_id 字段引用,其中类别集合在板集合中使用 cat_id 引用。这就是试图得到它的方式。

 Boards.find({_id:"ObjectId(55acec2687fe55f12ded5b4a)"},function(res){
if(res){

categories.find({_id:res.cat_id},function(res1){
if(res1){
users.find({_id:res.user_id},function(res3){
res.send(res3)
})

})
})

由于在 meteor 中使用 mongoose 会影响性能,所以我不能使用 populate 方法。那么有没有其他方法可以达到这个结果而不是以上一个呢?

最佳答案

可能 collection helpers .

基本用法:

Boards.helpers({
creator: function () {
return Meteor.users.findOne(this.creatorId);
},
category: function () {
return Categories.findOne(this.categoryId);
}
});

在模板中的使用非常简单。假设您有自己的董事会:

{{#each boards}}
<div>
<h3>{{board_name}}</h3>
<p>Created by</p>: {{ creator.username }}
<p>Category</p>: {{ category.catname }}
</div>
{{/each}}

添加提示:使用 publish-composite以更易于管理的方式发布关系。

Meteor.publishComposite('board', function (boardId) {
check(boardId, String);
return {
find: function () {
return Boards.find(boardId);
},
children: [{
find: function (board) {
return Meteor.users.find(board.creatorId);
}
}, {
find: function (board) {
return Categories.find(board.categoryId);
}
}]
}
});

关于mongodb - 用 meteor 填充在mongodb中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32694648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com