在mongoose中使用重命名集合



我有个问题。我将我的收藏回复重命名为旧回复。它工作得很好,但现在我需要检索我的数据,而我的障碍是我只使用该模型从集合中检索数据。但现在我需要从重命名的集合中检索数据,但我没有模型和模式。我试图创建一个模式和模型,但没有成功。它不返回任何元素。

这是代码的一部分:

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

最后我发现了如何做,也许这对来说是有用的

你只需要在你的模式中像这样精确地命名你的集合(收藏:"旧"(

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

最新更新