猫鼬重命名集合

Gon*_*nys 4 mongoose mongodb

我正在尝试使用mongoose制作db.collection.renameCollection,但我在任何地方都找不到该功能。他们错过添加它还是我看错了地方?
作为我正在做的事情的快速示例是:

var conn = mongoose.createConnection('localhost',"dbname");
var Collection = conn.model(collectionName, Schema, collectionName);
console.log(typeof Collection.renameCollection);
Run Code Online (Sandbox Code Playgroud)

其中显示未定义。

var con = mongoose.createConnection('localhost',"dbname");
con.once('open', function() {
    console.log(typeof con.db[obj.name]);
});
Run Code Online (Sandbox Code Playgroud)

这也未定义。

rob*_*lep 9

下面是一个使用 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();
});
Run Code Online (Sandbox Code Playgroud)

如您所见,MongoDB 驱动程序将renameCollection()方法公开为rename(),此处记录:http : //mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#rename