我试图用猫鼬制作db.collection.renameCollection,但我无法在任何地方找到这个功能。他们是否错过了添加它或者我正在寻找错误的地方? 作为我正在做的事的简单例子是:
var conn = mongoose.createConnection('localhost',"dbname");
var Collection = conn.model(collectionName, Schema, collectionName);
console.log(typeof Collection.renameCollection);
哪个节目未定义。
var con = mongoose.createConnection('localhost',"dbname");
con.once('open', function() {
console.log(typeof con.db[obj.name]);
});
这也是未定义的。
答案 0 :(得分:3)
这是一个使用Mongoose执行重命名操作的示例。
const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost/test').then(() => {
console.log('connected');
// Access the underlying database object provided by the MongoDB driver.
let db = mongoose.connection.db;
// Rename the `test` collection to `foobar`
return db.collection('test').rename('foobar');
}).then(() => {
console.log('rename successful');
}).catch(e => {
console.log('rename failed:', e.message);
}).then(() => {
console.log('disconnecting');
mongoose.disconnect();
});
正如您所看到的,MongoDB驱动程序将renameCollection()
方法公开为rename()
,此处记录在此:http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#rename