我有一个问题。我将我的收藏回复重命名为旧的。它工作得很好,但现在我需要检索我的数据,而我的障碍是我只使用该模型从集合中检索我的数据。但现在我需要从重命名的集合中检索数据,但我没有模型和架构。我尝试创建架构和模型,但没有成功。它不返回任何元素。
这是部分代码:
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'
},);
关于javascript - 在 Mongoose 中使用重命名集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51608481/