简体   繁体   English

在猫鼬中使用重命名集合

[英]Use Rename collection in mongoose

I have a problem. 我有个问题。 I renamed my collection responses into old. 我将收藏回复重命名为旧的。 It works really well but now I need to retrieve my data and my impediment is that I only used the model to retrieve my data from a collection. 它确实运行良好,但是现在我需要检索我的数据,而我的障碍是我仅使用模型从集合中检索我的数据。 But now I need to retrieve my data from my renamed collection but I have no model and schema. 但是现在我需要从重命名的集合中检索数据,但是我没有模型和架构。 I tried to create a schema and a model but it didn't work. 我试图创建一个模式和一个模型,但是没有用。 It returns no elements. 它不返回任何元素。

Here is a part of the code : 这是代码的一部分:

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);
    }
  });

Here is the part where it doesn't work: console.log(oldData) returns nothing. 这是不起作用的部分: console.log(oldData)返回任何内容。 And I know that my data is in the database when I try to retrieve them. 而且,当我尝试检索数据时,我知道我的数据在数据库中。

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);
    });
}

Finally I found how to do maybe it will be usefull for someone 终于我发现该怎么做也许对某人有用

You just need in your schema to precise the name of your collection like this ( collection : 'old' ) 您只需要在架构中像这样精确地定义集合的名称(collection:'old')

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' 
},);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM