
Collection::existsInDatabase 메소드는 deprecated 상태라 사용할 수 없습니다. 대신에, mongoose.model() 메소드를 사용하여 모델을 생성하고, 모델의 schema를 확인하여 Collection이 데이터베이스에 존재하는지 여부를 확인할 수 있습니다.
#hostingforum.kr
javascript
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });
const myModel = mongoose.model('myModel', new mongoose.Schema({
// schema definition
}));
if (mongoose.connection.db.collectionNames().includes('myModel')) {
console.log('Collection exists in database');
} else {
console.log('Collection does not exist in database');
}
또는, mongoose.connection.db.collectionNames() 메소드를 사용하여 데이터베이스에 존재하는 모든 Collection의 이름을 가져와, Collection이 데이터베이스에 존재하는지 여부를 확인할 수 있습니다.
#hostingforum.kr
javascript
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connection.db.collectionNames((err, collections) => {
if (collections.includes('myCollection')) {
console.log('Collection exists in database');
} else {
console.log('Collection does not exist in database');
}
});
2025-06-20 23:59