【问题标题】:Need some clarification on mongoose/mongodb populate command需要对 mongoose/mongodb 填充命令进行一些说明
【发布时间】:2016-11-03 04:51:13
【问题描述】:

您好,我正在制作一个包含用户和帖子的基本应用程序。

我遵循了关于人口 (http://mongoosejs.com/docs/2.7.x/docs/populate.html) 的猫鼬文档并设置了我的模式,以便用户并连接到帖子

var userSchema = new mongoose.Schema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
email:  String,
created_at: Date,
updated_at: Date,
admin: Boolean,
posts: [{ type: mongoose.Schema.ObjectId, ref: 'Post' }]
});

var postSchema = new mongoose.Schema({
_user : [{ type: mongoose.Schema.ObjectId, ref: 'User' }],
audioFile: { type: String, required: true },
imageFile: { type: String },
title: { type: String, required: true },
artist: { type: String, required: true },
start: { type: String, required: true },
stop: { type: String, required: true },
genre: { type: String, required: true },
tags: [{ type: String }]

});

app.get('/', function (req, res){
    Post.find({}, function(err, allPosts){
        if(!err){
            res.render('main.njk', {
                posts : allPosts,
                title : 'Title',
                isLogged : req.session.isLogged,
                user : req.session.user,
                messages : req.flash('alert')
            });
        } else { return done(err); }
    });

});

这一切都很好,我可以在 allPosts 上运行一个 foreach 循环以将每一个都提取到我的 HTML 中,但是当我尝试考虑如何显示所有帖子时,我将各自的用户附加到每个帖子上我不确定如何将两者联系起来,因为 mongoose 文档中的所有示例都主要用于 findOne。

我在想这样的事情

app.get('/', function (req, res){
    Post.find({}, function(err, allPosts){
        if(!err){
            allPosts.populate('_user', ['username']);
            allPosts.exec(function (err, users){
                if(err) console.log(err);
                console.log(users); 
            });
            res.render('main.njk', {
                posts : allPosts,
                title : 'Spaurk.net',
                isLogged : req.session.isLogged,
                user : req.session.user,
                messages : req.flash('alert')
            });
        } else { return done(err); }
    });

});

但这当然行不通。

所以我想知道是否有对此情况有经验的人能够帮助我解决这个问题。

非常感谢您的任何意见。

编辑,感谢 Daves 的帮助,我能够让填充正常工作,我只是无法正确提取我想要的字段

    Post.find({}).populate('_user').exec(function(err, allPosts){

在我的循环中 {% for post in posts %} ,当我做 post._user 它显示整个用户模式,但是当我做 post._user.username 它不返回任何东西。我不确定这是为什么。

【问题讨论】:

  • 您需要将查询更改为:Post.find({}).populate('_user').exec((err, allposts){...})。这将填充所有Post 文档user 属性并将其作为一个文档集合返回给您。
  • 您好 Dave,感谢您的回复并清理语法。当我在我的 foreach 循环中调用 post._user.username 时,我什么也得不到,但是当我调用 post._user 时,它给了我用户的整个对象,由于某种原因,我无法单独访问每个字段。再次感谢您的帮助
  • 你在_user数组上做foreach吗?
  • {% for post in posts %} 是我所拥有的。我也应该把它放在下面吗,{% for user in posts._user %}?
  • 啊,是的,_user 是一个数组,所以如果你想访问单个属性,你必须遍历它,或者指定你想使用数组中的哪个对象,例如:post._user[0].username

标签: node.js mongodb express mongoose populate


【解决方案1】:

在查询中构建填充的正确方法如下:

Post.find({})
    .populate('_user')
    .exec((err, allposts){...})

然后,您将拥有一个 Posts 数组,其中填充了 _user 数组。如果您需要访问用户的属性,您将需要通过 _user 数组进行另一个循环,或者指定您要使用 _user[0].<property>

【讨论】:

  • 现在完美运行。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 2016-06-16
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
相关资源
最近更新 更多