#创建新的集合yb
> for(i=0;i<10;i++){db.yb.insert({'i':i})} WriteResult({ "nInserted" : 1 }) > show collections
#重命名集合为yb2
> db.yb.renameCollection('yb2'); { "ok" : 1 } > show collections yb2 >
跨数据库重名集合
> use test switched to db test > show collections yb1 > use test2 switched to db test2 > show collections > db.runCommand({renameCollection:"test.yb1",to:"test2.yb",dropTarget:true}); { "ok" : 0, "errmsg" : "renameCollection may only be run against the admin database.", "code" : 13 } > show collections > use admin switched to db admin > db.runCommand({renameCollection:"test.yb1",to:"test2.yb",dropTarget:true}); { "ok" : 1 } >
nodejs 中修改集合名称 数据库为users 原集合名称abc 修改成def
var MongoClient = require('mongodb').MongoClient; //定义mongodb服务器连接地址 var mongoUrl = "mongodb://localhost:27017/"; MongoClient.connect(mongoUrl,function(err,db){ if (err) throw err; var dbo = db.db("users"); //修改集合名称 dbo.collection('abc').rename('def',function(err,dda){ console.log(dda); }) //获取所有集合名称 dbo.admin().listDatabases(function(err,dbs){ console.log(dbs); }) })