Mongoose DBrefs - 转换为ObjectId失败的值

时间:2013-02-06 20:22:53

标签: javascript mongodb mongoose

我有一个团队架构,其中包含有关团队的详细信息,以及用于存储这些团队的匹配架构。我正在努力使匹配架构中的主队/客队参与Team对象。我已将我的代码放在下面,我在保存团队时遇到错误,但我不禁觉得我在Schema或者保存Match方面做错了。有人可以帮忙吗?

到目前为止,我有以下代码:

Team.js extract

var Team = new Schema({
  'key' : {
    unique : true,
    type : Number,
    default: getId
  },
  'name' : { type : String,
              validate : [validatePresenceOf, 'Team name is required'],
              index : { unique : true }
            }
});

module.exports.Schema = Team;
module.exports.Model = mongoose.model('Team', Team);

Match.js extract

var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Team = require('../schemas/Team').Schema;

var Match = new Schema({
  'key' : {
    unique : true,
    type : Number,
    default: getId
  },
  'hometeam' : {
    type : Schema.ObjectId,
    ref : 'Team'
  },
  'awayteam' : {
    type : Schema.ObjectId,
    ref : 'Team'
  }
});

module.exports = mongoose.model('Match', Match);

index.js

  app.get('/match', function(req, res) {
    var key = 1356136550152; // Reading
    Team.findByKey(key, function(err, team) {
      if(err) {
        res.send("An error occured");
      }
      if(!team) { 
        res.send("The team does not exist");
      }
      var match = new Match();
      match.hometeam = team;
      match.save(function(err) {
        if(err) {
          util.log('Error while saving Match: ' + util.inspect(err));
          res.send("An error occured whilst saving the match");
        } else {
          res.send("Saved the match");
        }
      });
    });
  });

错误:

Error while saving Match: { message: 'Cast to ObjectId failed for value "{ name: \'testTeam\',\n  _id: 50d500663ca6067226000001,\n  __v: 0,\n  key: 1356136550152 }" at path "hometeam"',
  name: 'CastError',
  type: 'ObjectId',
  value: 
   [ { name: 'testTeam',
       _id: 50d500663ca6067226000001,
       __v: 0,
       key: 1356136550152 } ],
  path: 'hometeam' }

team._id错误

Error while saving Match: { [MongoError: E11000 duplicate key error index: testdb.matches.$team.name_1  dup key: { : null }]
  name: 'MongoError',
  err: 'E11000 duplicate key error index: testdb.matches.$team.name_1  dup key: { : null }',
  code: 11000,
  n: 0,
  connectionId: 8,
  ok: 1 }

db.matches.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "testdb.matches",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "key" : 1
        },
        "unique" : true,
        "ns" : "testdb.matches",
        "name" : "key_1",
        "background" : true,
        "safe" : null
    },
    {
        "v" : 1,
        "key" : {
            "team.key" : 1
        },
        "unique" : true,
        "ns" : "testdb.matches",
        "name" : "team.key_1",
        "background" : true,
        "safe" : null
    }
]

1 个答案:

答案 0 :(得分:2)

index.js中应该是:

match.hometeam = team._id;

而不是:

match.hometeam = team;

<强>更新

关于新的错误消息,看起来您在matches集合上有一个引用不存在的字段的唯一索引。使用以下命令将其放入shell中:

db.matches.dropIndex('team.name_1')