在同一数据库中复制集合的最快方法是什么? [英] What's the fastest way to copy a collection within the same database?

查看:61
本文介绍了在同一数据库中复制集合的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在相同数据库中复制一个集合,并给它一个 名称不同-基本是拍摄快照.

I want to copy a collection within the same database and give it a different name - basically take a snapshot.

执行此操作的最佳方法是什么?有命令吗,还是我必须 依次复制每条记录?

What's the best way to do this? Is there a command, or do I have to copy each record in turn?

我知道cloneCollection命令,但这似乎是为了 仅复制到另一台服务器.

I'm aware of the cloneCollection command, but it seems to be for copying to another server only.

我也知道mongoimportmongoexport,但是当我通过PHP进行此操作时,我不想对外壳进行调用.

I'm also aware of mongoimport and mongoexport, but as I'm doing this via PHP I'd prefer not to make calls out to the shell.

推荐答案

您有几种选择,但是最快的是:

You have a few options, but the fastest is:

mongodump -d db -c sourcecollection 
mongorestore -d db -c targetcollection --dir=dump/<db>/<sourcecollection.bson>

mongoexport -d db -c sourcecollection | mongoimport -d db -c targetcollection --drop

或在php中:

`mongoexport -d db -c sourcecollection | mongoimport -d db -c targetcollection --drop`;

在那之后

mongo db < script.js

在何处,如 mongo文档,script.js包含以下内容:

where, as shown in the mongo docs, script.js contains something like:

db.myoriginal.find().forEach( function(x){db.mycopy.insert(x)} );

最慢(一个数量级或更多)复制集合的方法将是使用本机php驱动程序-仅仅是因为移动信息.但是,如果您绝对想避免使用 db execute 函数.

The slowest (by an order of magnitude or more) way to copy a collection will be to use the native php driver - simply because of moving information around. But you could issue the above mongo query if you absolutely want to avoid cli calls using the db execute function.

这篇关于在同一数据库中复制集合的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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