【问题标题】:How to populate mongoose with obejectId which is defined as Number?如何用定义为数字的obejectId填充猫鼬?
【发布时间】:2022-02-08 20:10:13
【问题描述】:

我正在尝试填充我的用户 requests.profileId 但它只返回空值。

我有以下架构:

第一个架构:

const profileSchema = new mongoose.Schema({
  _id: { type: Number }, //<- _id is defined as a number which represents mobile number (easier for me to handle)
  first: { type: String },
  second: { type: String },
});

module.exports = mongoose.model('Profile', profileSchema, 'profiles');

第二个架构:

const userSchema = new mongoose.Schema({
  firstName: { type: String },
  lastName: { type: String },
  requests: [
    {
      profileId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Profile',
      },
      requestTime: { type: Date, default: Date.now },
    },
  ],
});

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

这是我的代码:

const user = await User.findById(req.user).populate('requests.profileId');
console.log(user.requests);

这是输出:

[
  {
    _id: 6201633869648e2b74c00a10,
    profileId: null,
    requestTime: 2022-02-07T18:21:44.722Z
  },
  {
    _id: 6201633b69648e2b74c00a11,
    profileId: null,
    requestTime: 2022-02-07T18:21:47.238Z
  },
  {
    _id: 620238f9d2b5dd3dee6c41a2,
    profileId: null,
    requestTime: 2022-02-08T09:33:45.176Z
  },
  {
    _id: 620239253220343dfd7cfdd9,
    profileId: null,
    requestTime: 2022-02-08T09:34:29.780Z
  }
]

这是没有填充的输出:

[
  {
    _id: 6201633869648e2b74c00a10,
    profileId: 393732353235303134343330, //<- typeof profileId is obeject
    requestTime: 2022-02-07T18:21:44.722Z
  },
  {
    _id: 6201633b69648e2b74c00a11,
    profileId: 393732353435353333313131,
    requestTime: 2022-02-07T18:21:47.238Z
  },
  {
    _id: 620238f9d2b5dd3dee6c41a2,
    profileId: 393732353435353333313131,
    requestTime: 2022-02-08T09:33:45.176Z
  },
  {
    _id: 620239253220343dfd7cfdd9,
    profileId: 393732353435353333313131,
    requestTime: 2022-02-08T09:34:29.780Z
  }
]

目前 Profile.findById(mobileNumber) 工作正常。

任何想法出了什么问题?

非常感谢您的帮助。

提前致谢:)

【问题讨论】:

    标签: javascript mongodb mongoose mongoose-populate


    【解决方案1】:

    试试这个可能行得通,如果不行,请告诉我

    const user = await User.findById(req.user).populate('Profile');
    console.log(user.requests); 
    

    【讨论】:

    • 嗨,谢谢,但它不起作用。仍然得到:[{ _id: 620239253220343dfd7cfdd9, profileId: 393732353435353333313131, requestTime: 2022-02-08T09:34:29.780Z }]
    【解决方案2】:

    试试这个:

    User.findById(req.user).populate({
        path: 'requests',           
        populate: {
            path:  'profileId',
            model: 'Profile' 
        }
      })
    

    【讨论】:

    • 嗨,谢谢,但它不起作用
    【解决方案3】:

    对于有同样问题的未来读者!

    我已经找到了解决这个问题的方法。

    我必须在我的 userSchema 中将 profileId 类型更改为 Number:

    const userSchema = new mongoose.Schema({
      firstName: { type: String },
      lastName: { type: String },
      requests: [
        {
          profileId: {
            type: Number // and NOT use type: mongoose.Schema.Types.ObjectId,
            ref: 'Profile',
          },
          requestTime: { type: Date, default: Date.now },
        },
      ],
    });
    
    module.exports = mongoose.model('User', userSchema, 'users');
    

    现在可以了!

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 1970-01-01
      • 2020-06-28
      • 2017-10-11
      • 2021-05-06
      • 2015-07-13
      • 2014-04-19
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多