【问题标题】:How To Create Mongoose Schema with Array of Object IDs?如何使用对象 ID 数组创建 Mongoose 模式?
【发布时间】:2014-04-10 06:22:35
【问题描述】:

我已经定义了一个猫鼬用户模式:

var userSchema = mongoose.Schema({
  email: { type: String, required: true, unique: true},
  password: { type: String, required: true},
  name: {
      first: { type: String, required: true, trim: true},
      last: { type: String, required: true, trim: true}
  },
  phone: Number,
  lists: [listSchema],
  friends: [mongoose.Types.ObjectId],
  accessToken: { type: String } // Used for Remember Me
});

var listSchema = new mongoose.Schema({
    name: String,
    description: String,
    contents: [contentSchema],
    created: {type: Date, default:Date.now}
});
var contentSchema = new mongoose.Schema({
    name: String,
    quantity: String,
    complete: Boolean
});

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

friends 参数定义为对象 ID 数组。 换句话说,一个用户将拥有一个包含其他用户 ID 的数组。我不确定这是否是这样做的正确符号。

我正在尝试将一个新朋友推送到当前用户的朋友数组中:

user = req.user;
  console.log("adding friend to db");
  models.User.findOne({'email': req.params.email}, '_id', function(err, newFriend){
    models.User.findOne({'_id': user._id}, function(err, user){
      if (err) { return next(err); }
      user.friends.push(newFriend);
    });
  });

但是这给了我以下错误:

TypeError:对象 531975a04179b4200064daf0 没有方法“cast”

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    我自己是 Mongoose 的新手,所以我不完全确定这是对的。但是,您似乎写过:

    friends: [mongoose.Types.ObjectId],
    

    我相信您正在寻找的房产实际上是在这里找到的:

    friends: [mongoose.Schema.Types.ObjectId],
    

    不过,自从您发布此问题以来,文档可能已经发生了变化。抱歉,如果是这样的话。请参阅the Mongoose SchemaTypes docs 了解更多信息和示例。

    【讨论】:

      【解决方案2】:

      如果你想使用 Mongoose 填充功能,你应该这样做:

      var userSchema = mongoose.Schema({
        email: { type: String, required: true, unique: true},
        password: { type: String, required: true},
        name: {
            first: { type: String, required: true, trim: true},
            last: { type: String, required: true, trim: true}
        },
        phone: Number,
        lists: [listSchema],
        friends: [{ type : ObjectId, ref: 'User' }],
        accessToken: { type: String } // Used for Remember Me
      });
      exports.User = mongoose.model('User', userSchema);
      

      这样你就可以做这个查询了:

      var User = schemas.User;
      User
       .find()
       .populate('friends')
       .exec(...)
      

      您会看到每个用户都有一组用户(该用户的朋友)。

      而正确的插入方式就像Gabor说的:

      user.friends.push(newFriend._id);
      

      【讨论】:

      • 但是它创建了一个看起来像[{"$oid": "dfdfsdfsg565ls"},...]而不是["hkugh87tgkgk", ....]的数组
      • 这就是它应该的样子,一个ObjectIds的列表。 Mongoose 已经处理好了。
      • populate() 到底是做什么的?
      • @KennyWorden 来自 mongoose 的填充方法模拟了传统关系数据库中的连接。所以在这种情况下,填充将用用户文档数组替换朋友数组。
      • 嘿,@RodrigoReis 你能告诉我如何写查询,如果我想命名 -> first = 'ajay'; ` 我大致写了这个,但没有得到任何结果。
      【解决方案3】:

      我会试试这个。

      user.friends.push(newFriend._id);
      

      friends: [userSchema],
      

      但我不确定这是否正确。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        • 2018-06-11
        • 1970-01-01
        • 2018-11-30
        • 2017-09-30
        • 1970-01-01
        • 2021-11-04
        相关资源
        最近更新 更多