猫鼬重命名集合 [英] Mongoose rename collection

查看:38
本文介绍了猫鼬重命名集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make db.collection.renameCollection with mongoose, but i can't find that function anywhere. Did they miss to add it or i am looking at wrong place?
As quick example of what i am doing is:

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

这也未定义.

推荐答案

这是一个使用 Mongoose 执行重命名操作的示例.

Here's an example that will perform a rename operation using 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#重命名

As you can see, the MongoDB driver exposes the renameCollection() method as rename(), which is documented here: http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#rename

这篇关于猫鼬重命名集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆