使用 mongoDB 重命名集合 [英] renaming a collection with mongoDB

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

问题描述

我无法在 mongoDB 中重命名集合.我可以看到它存在并且可以从中写入和读取数据.我已尝试使用 node mongo 本机驱动程序进行以下操作.

I can not rename a collection in mongoDB. I can see that it exists and can write and read data from it. I have attempted the following using the node mongo native driver.

db.collection("mycollection").renameCollection("mynewcollection");

error: TypeError: Object #<Collection> has no method 'renameCollection'

db['mycollection'].renameCollection("mynewcollection");

Cannot call method 'renameCollection' of undefined

在同一个地方执行以下操作会按预期返回所有文档

performing the following in the same place returns all docs as expected

db.collection("mycollection").find({}).toArray(function(err, docs){
    console.log(docs);
});

推荐答案

使用 node.js 驱动重命名集合的方法是 rename,而不是 renameCollection:

The method to rename a collection using the node.js driver is rename, not renameCollection:

db.collection("mycollection").rename("mynewcollection", function(err, newColl) {...});

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

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