有什么简单的方法吗?
当前回答
使用pymongo,你需要在同一个mongod上有两个数据库,我做了以下工作:
Db =原始数据库 Db2 =要复制到的数据库
cursor = db["<collection to copy from>"].find()
for data in cursor:
db2["<new collection>"].insert(data)
其他回答
我通常会这样做:
use sourcedatabase;
var docs=db.sourcetable.find();
use targetdatabase;
docs.forEach(function(doc) { db.targettable.insert(doc); });
你可以随时使用Robomongo。从v0.8.3开始,有一个工具可以通过右键单击集合并选择“将集合复制到数据库”来完成此操作。
具体操作请参见http://blog.robomongo.org/whats-new-in-robomongo-0-8-3/
由于这个特性在0.8.5版本中被移除,所以如果你想尝试的话,你将不得不使用0.8.3或0.8.4。
在MongoDB中将一个集合(myCollection1)从一个数据库复制到另一个数据库,
**Server1:**
myHost1.com
myDbUser1
myDbPasword1
myDb1
myCollection1
outputfile:
myfile.json
**Server2:**
myHost2.com
myDbUser2
myDbPasword2
myDb2
myCollection2
你可以这样做:
mongoexport --host myHost1.com --db myDb1 -u myDbUser1 -p myDbPasword1 --collection myCollection1 --out myfile.json
然后:
mongoimport --host myHost2.com --db myDb2 -u myDbUser2 -p myDbPasword2 --collection myCollection2 --file myfile.json
另一种情况,使用CSV文件:
Server1:
myHost1.com
myDbUser1
myDbPasword1
myDb1
myCollection1
fields.txt
fieldName1
fieldName2
outputfile:
myfile.csv
Server2:
myHost2.com
myDbUser2
myDbPasword2
myDb2
myCollection2
你可以这样做:
mongoexport --host myHost1.com --db myDb1 -u myDbUser1 -p myDbPasword1 --collection myCollection1 --out myfile.csv --type=csv
在CSV文件中添加列类型(name1.decimal(),name1.string()..),然后:
mongoimport --host myHost2.com --db myDb2 -u myDbUser2 -p myDbPasword2 --collection myCollection2 --file myfile.csv --type csv --headerline --columnsHaveTypes
使用pymongo,你需要在同一个mongod上有两个数据库,我做了以下工作:
Db =原始数据库 Db2 =要复制到的数据库
cursor = db["<collection to copy from>"].find()
for data in cursor:
db2["<new collection>"].insert(data)
使用“Studio3T for MongoDB” 通过单击数据库,集合或特定的集合具有导出和导入工具 下载链接:https://studio3t.com/download/
推荐文章
- JSON文件的蒙古导入
- 如何删除mongodb中的数组元素?
- 修改MongoDB数据存储目录
- 在MongoDB中查找重复的记录
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 在猫鼬,我如何排序的日期?(node . js)
- 将映像存储在MongoDB数据库中
- 重复Mongo ObjectId的可能性在两个不同的集合中生成?
- Redis比mongoDB快多少?
- 无法连接到服务器127.0.0.1:27017
- 如何创建数据库的MongoDB转储?
- 如何将MongoDB作为Windows服务运行?
- 如何监听MongoDB集合的变化?
- 如何在猫鼬排序?
- js的Mongoose.js字符串到ObjectId函数