【问题标题】:Mongoose - Find Model by the author's idMongoose - 通过作者的 id 查找模型
【发布时间】:2021-10-31 04:39:27
【问题描述】:

我正在尝试通过作者的 id 查找给定作者创建的所有帖子,但我一直得到一个空数组

下面是模型和获取请求

let campgroundSchema = new mongoose.Schema({
    name:String,
    image:String,
    location:String,
    lat:Number,
    lng:Number,
    price:String,
    description:String,
    createdAt: {type:Date, default:Date.now},
    author:{
        id:{
            type:mongoose.Schema.Types.ObjectId,
            ref:'User'
        },
        username:String,
    },
    comments:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Comment'
    }]
});

let Campground = mongoose.model('campground',campgroundSchema);```

const userSchema = new mongoose.Schema({
    username:String,
    password:String,
    firstName:String,
    lastName:String,
    email:String,
    profileImage:String,
    isAdmin:{type:Boolean, default:false},
    followers:[
        {
            type:mongoose.Schema.Types.ObjectId,
            ref:'User'
        }
    ],
    notifications:[{
        type:mongoose.Schema.Types.ObjectId,
        ref:'Notification'
    }]
});

userSchema.plugin(passportLocalMongoose);
const User = mongoose.model('User', userSchema);

您好,我是否通过作者的 id 获得给定作者的所有露营地数组?这个想法是呈现一个用户个人资料页面,其中包含存储在用户模型中的用户详细信息以及用户创建的帖子数量.

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    帖子集合中的简单搜索

    Post.find({author.id: req.params.id},(err, posts) => 
        {
           ....
        }) 
    

    【讨论】:

    • 这个想法是呈现一个用户个人资料页面,其中包含存储在用户模型中的用户详细信息以及用户创建的帖子数量。我想先找到用户,然后通过作者 id 找到相关的露营地,这与找到的用户的 id 相同。
    【解决方案2】:

    如果我理解正确,您正试图按作者 ID 归还所有露营地。如果是这种情况,则您的查询正在查找错误的 ID。

    User.findById(req.params.id)
    

    将在用户集合中查找与 Mongo 自动生成的本机 _id 匹配的文档。

    如果您想查找与作者 ID 匹配的露营地,您的查询应该类似于:

    User.find({ author.id: req.params.id })
    

    希望这会有所帮助!

    【讨论】:

    • 这个想法是呈现一个用户个人资料页面,其中包含存储在用户模型中的用户详细信息以及用户创建的帖子数量。我想先找到用户,然后通过作者 id 找到相关的露营地,这与找到的用户的 id 相同。
    • 看看猫鼬填充函数mongoosejs.com/docs/populate.html这可能就是你要找的东西
    • 感谢帮助
    【解决方案3】:

    我解决了。

    router.get('/users/:id',(req,res) => {
         User.findById(req.params.id).populate('posts').exec((err,foundUser)=>{
            if (err) {
                req.flash('error_msg',"Something went wrong");
                res.redirect('/');
            } else {
                Campground.find({'author.id':foundUser.id},(err,foundUser_campgrounds)=>{
                    res.render('users/show', {userProfile:foundUser, userCampgrounds:foundUser_campgrounds});
                });
            }
    
        });
    });
    

    【讨论】:

    • 问题架构中定义的posts 的帖子在哪里?
    【解决方案4】:
    const User=require('./models/user');
    const Campground = require('./models/campgrounds');
    
    app.get('/profile/:id',(req,res) => {
        const user = User.findById(req.params.id);
        const camp= Campground.find({author:user._id});
        res.send(camp);
    });
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。您可以在帮助中心找到更多关于如何写好答案的信息:stackoverflow.com/help/how-to-answer。祝你好运?
    猜你喜欢
    • 2022-11-11
    • 2021-05-29
    • 2023-02-17
    • 1970-01-01
    • 2021-03-23
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多