在猫鼬中使用重命名集合

时间:2018-07-31 08:10:26

标签: javascript node.js mongoose find rename

我有问题。我将收藏回复重命名为旧的。它确实运行良好,但是现在我需要检索我的数据,而我的障碍是我仅使用模型从集合中检索我的数据。但是现在我需要从重命名的集合中检索数据,但是我没有模型和架构。我试图创建一个模式和一个模型,但是没有用。它不返回任何元素。

这是代码的一部分:

app.get("/Play", function(req, res) {
  var urlTempBox = 'http://localhost:3000/Play';

  ///////////////////////////////////////////////////////////
  request(urlTempBox, function(error, response, body) {
    if (error) {
      throw (error);
    } else {
      var jobj = JSON.parse(response.body);
      persistRS(jobj);

      setTimeout(function() {
        ResponseDatabase.find()
          .populate('unitCode')
          .exec(function(err, finalData) {
          if (err) throw (err);
          mongoose.connection.db.listCollections({
            name: 'old'
          })
            .next(function(err, collinfo) {
            if (err) throw (err);
            if (collinfo) {
              console.log('lookinOld');
              OldResponseDatabase.find()
                .populate('unitCode')
                .exec(function(err, oldData) {
                if (err) throw (err);
                console.log('itsOld');
                console.log(oldData);
                res.send(finalData);
              });
            } else {
              console.log('fk');
              res.send(finalData);
            }
          })
        })
      }, 5000);
    }
  });

这是不起作用的部分:console.log(oldData)不返回任何内容。而且,当我尝试检索数据时,我知道我的数据在数据库中。

if (collinfo) {
  console.log('lookinOld');
  OldResponseDatabase.find()
    .populate('unitCode')
    .exec(function(err, oldData) {
      if (err) throw (err);
      console.log('itsOld');
      console.log(oldData);
      res.send(finalData);
    });
}

1 个答案:

答案 0 :(得分:0)

最后,我发现该怎么做可能对某人有用

您只需要在架构中像这样精确地定义集合的名称 (收藏:“旧”)

var nameSchemaOldRS = new mongoose.Schema({
  MainClass: String,
  BookingClass: String,
  carID: String,
  unitCode:{type: String, ref: 'Map' ,required: [true,'No post id found']},
  Deck:Number,
  Orientation:String,
  Position:String, 
  }, {
  versionKey: false,
  collection : 'old' 
},);