mongoose.populate() 未显示填充内容

Posted

技术标签:

【中文标题】mongoose.populate() 未显示填充内容【英文标题】:mongoose.populate() is not showing populate content 【发布时间】:2021-02-17 21:22:49 【问题描述】:

//我的用户架构

const mongoose = require('mongoose');

const ChatRoom = 要求('./chatRoom');

const userSchema = new mongoose.Schema(

_id: mongoose.Schema.Types.ObjectId,
username:
    type: 'String',
    unique: true,
,
collegeEmail:
    type: String,
    unique: true,
,
password: String,
photo: String,
name: String,
phoneNo: Number,
collegeName: String,
gender: String,
chatList:[userId:this, chatId:type: mongoose.Schema.Types.ObjectId, ref: 'ChatRoom'],
bio: String,
follow:[ this],
following:[ this],
lastSeen: Number,
active: Boolean, 
status: Boolean,
otp: Number

);

const User = mongoose.models.User || mongoose.model('用户', userSchema); module.exports = 用户;

//chatRoom 架构

const mongoose = require('mongoose');

const User = require('./user');

//msg 架构

const chatMsgSchema = new mongoose.Schema(

_id: mongoose.Schema.Types.ObjectId,
sender:  type: mongoose.Schema.Types.ObjectId, ref: 'User' ,
receiver:  type: mongoose.Schema.Types.ObjectId, ref: 'User' ,
msg: String,
time: Number

);

const ChatMsg = mongoose.models.ChatMsg || mongoose.model('ChatMsg', chatMsgSchema);

//chatTable 架构

const chatTableSchema = new mongoose.Schema(

_id: mongoose.Schema.Types.ObjectId,
chats:[type: mongoose.Schema.Types.ObjectId, ref: 'ChatMsg'],

);

const ChatRoom = mongoose.models.ChatRoom || mongoose.model('ChatRoom', chatTableSchema);

module.exports = ChatRoom, ChatMsg;

// 我用于填充聊天列表的脚本

router.post('/room',ensureAuthenticated,async function(req, res)

const id = req.body.id;
console.log(id);
const frnd = await User.
    findOne(username: req.user.username).
    populate(
        path: 'chatList',
        model: 'ChatRoom',
        match:  _id: id
    ).
    exec();
    console.log(frnd);

);

在控制台上显示所有聊天列表 =>

我对 chatList 应用的这些工作既没有填充也没有过滤

【问题讨论】:

【参考方案1】:
    欢迎使用 Stack Overflow 向 SO 提交问题时,请格式化您的问题,以便代码和消息之间有明显的区别。

尝试为您提供答案:您的代码存在很多问题。

你不应该在你的架构中定义_id,除非你有更具体的问题。

更简洁的代码是:

// my user schema

import mongoose,  Schema  from 'mongoose';

const userSchema = new Schema(
  username: 
    type: String,
    unique: true,
  ,
  collegeEmail: 
    type: String,
    unique: true,
  ,
  password:  type: String ,
  photo:  type: String ,
  name:  type: String ,
  phoneNo:  type: Number ,
  collegeName:  type: String ,
  gender:  type: String ,
  follow: [type: mongoose.Schema.Types.ObjectId, ref: 'User'],
  following: [type: mongoose.Schema.Types.ObjectId, ref: 'User'],
  chatList: [userId:type: mongoose.Schema.Types.ObjectId, ref: 'User', chatId:type: mongoose.Schema.Types.ObjectId, ref: 'ChatRoom'],
  bio:  type: String ,
  lastSeen:  type: Number ,
  active:  type: Boolean ,
  status:  type: Boolean ,
  otp:  type: Number ,
);

const User = mongoose.model('User', userSchema);
module.exports = User;

//msg schema

const chatMsgSchema = new Schema(
  sender:  type: Schema.Types.ObjectId, ref: 'User' ,
  receiver:  type: Schema.Types.ObjectId, ref: 'User' ,
  msg:  type: String ,
  time:  type: Number ,
);

const ChatMsg = mongoose.model('ChatMsg', chatMsgSchema);

//chatTable schema

const chatTableSchema = new Schema(
  chats: [ type: Schema.Types.ObjectId, ref: 'ChatMsg' ],
);

const ChatRoom = mongoose.model('ChatRoom', chatTableSchema);

module.exports =  ChatRoom, ChatMsg ;

// my script for populate chatList

router.post('/room', ensureAuthenticated, async function(req, res) 
  const id = req.body.id;
  console.log(id);
  const frnd = await User.findOne( username: req.user.username )
    .populate('chatList')
    .exec();
  console.log(frnd);
);

编辑

您填充数据的方式有点不对劲 应该是:

router.post('/room', ensureAuthenticated, async function(req, res) 
  const id = req.body.id;
  console.log(id);
  const frnd = await User.findOne( username: req.user.username )
    .populate(
       path : 'chatList.chatId', 
       model : 'ChatRoom'
    )
    .populate(
       path : 'chatList.userId', 
       model : 'User'
    )
    .exec();
  console.log(frnd);
);

您可能需要调整样本。

【讨论】:

兄弟实际上我需要chatList中的userId,那么我该如何存储它。 首先,userid 不等于this。其次,这不能在模式定义中设置。第二:由于您从用户那里获取聊天列表,因此您已经有了 UserId。我将编辑我的答案,为您提供一个简单的示例。 this => 类型:mongoose.Schema..Type.ObjectId. 像关注和关注数组我使用了这个关键字,它正在工作。并且在 userId 中,我没有存储我自己的 id。这样,我想与之聊天的用户的 id 将受到保护。 好的,我现在更了解你的想法了。我从未在架构定义中使用 this 关键字,我习惯于使用模型和对象 ID 进行引用。我将编辑我的答案以严格回答您的问题。

以上是关于mongoose.populate() 未显示填充内容的主要内容,如果未能解决你的问题,请参考以下文章