每个人

所以我有一个"博客"应用程序,用户可以在其中创建带有图像的帖子.

这是我的用户模式

    import mongoose from "mongoose";
    import mongooseUniqueValidator from "mongoose-unique-validator";
    
    const Schema = mongoose.Schema;
    const validator = mongooseUniqueValidator;
    
    const user_Schema = new Schema({
            username: { type: String, required: true, unique: true },
            email: { type: String, required: true, unique: true },
            password: { type: String, required: true, minlength: 3 },
            user_image: { type: String },
            posts: [{ type: mongoose.Types.ObjectId, required: true, ref: 'Post'  }], //relation bewtean post and user
    
        },{ timestamps: true }
        );
    
    user_Schema.plugin(validator);
    
    
    export const USER: mongoose.Model<any> = mongoose.model("User", user_Schema);

这是我的帖子模式:

import mongoose from "mongoose";

const Schema = mongoose.Schema;

const post_Schema = new Schema({
        title: { type: String, required: true, },
        description: { type: String, required: true, },
        imageURL: { type: String },
        creator_id: { type: mongoose.Types.ObjectId, required: true, ref: 'User'  }, 
    },
    { timestamps: true }
    );

export const POST: mongoose.Model<any> = mongoose.model("Post", post_Schema);

然而,这就是我希望帖子包含的内容,我希望帖子包含创建它的用户的ID和创建它的用户的名称.

const Schema = mongoose.Schema;

const post_Schema = new Schema({
        title: { type: String, required: true, },
        description: { type: String, required: true, },
        imageURL: { type: String },
        user: {
            creator_id: { type: mongoose.Types.ObjectId, required: true, ref: 'User'  }, //relation bewtean post and user
            creator_name: { <soemthing here>, ref: 'User'  }, //relation bewtean post and user
        },
        
    },
    { timestamps: true }
    );

export const POST: mongoose.Model<any> = mongoose.model("Post", post_Schema);

推荐答案

如果要使用ref来检索创建者名称是正确的方法,则在检索Post个文档时只需填充以下内容:

const posts = await Post.find({}).populate('creator').exec()

for (const post of posts) {
    // Every post should contain the creator user properties
    console.log(post.creator._id, post.creator.username)
}

只需确保ref个字段的类型为mongoose.Schema.Types.ObjectId:

const post_Schema = new Schema(
    {
      title: { type: String, required: true },
      description: { type: String, required: true },
      imageURL: { type: String },
      creator: { type: mongoose.Schema.Types.ObjectId, required: true, ref: 'User' },
    },
    { timestamps: true }
  );
  
  export const POST: mongoose.Model<any> = mongoose.model('Post', post_Schema);

Mongodb相关问答推荐

使用查询参数过滤MongoDB Go驱动程序时出现问题

MongoDB Aggregate:查找每个月的交叉日期范围的数量

如何在MongoDB中通过限制和跳过查找项进行匹配

MongoDB v4.4聚合的$getfield替代方案

Mongo查找条件:不存在

发布到 MongoDB 时生成的附加id字段

MongoDB 聚合 - 条件 $lookup 取决于字段是否存在

Nestjs中不同语言数据的mongodb聚合代码

从 MongoDB find() 结果集中识别最后一个文档

Mongo:统计一组文档中单词出现的次数

Node.js 和 Passport 对象没有方法 validPassword

RoR3 上的 Mongoid:1)如何在查询中返回特定字段? 2)需要什么 inverse_of ?

为什么使用 Redis 而不是 MongoDb 进行缓存?

Mongo按实际上是数字的字符串值排序

无法在 mac os 10.9 上安装 mongodb php 驱动程序

show dbs 给出Not Authorized to execute command错误

试图从 mongoose 获取Collection 列表

如何从mongoose中的对象 ID 获取创建日期?

MongoDB 引用的最佳实践

如何从集合中删除除 MongoDB 中的文档之外的所有文档