在mongodb中创建空集合 [英] Creating empty collection in mongodb

查看:504
本文介绍了在mongodb中创建空集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将MongoDB集合从一个数据库复制到其他数据库.如果源集合中没有数据,我需要创建一个 empty 集合.在nodejs中如何使用猫鼬libraray做到这一点?

I need to copy a MongoDB collection from one database to some other database. I need to create an empty collection if there was no data in the source collection. How to do that in nodejs using mongoose libraray?

推荐答案

将集合从数据库复制到另一个数据库的最有效方法可能是使用从您的外壳> mongorestore :

Probably the most efficient way of copying a collection from a DB to an other is to use mongodump/mongorestore from your shell:

sh$ echo 'db.createCollection("some_collection")' | mongo my_dst_db 
sh$ mongodump  --db my_src_db --collection some_collection --out=- | \
    mongorestore --db my_dst_db --collection some_collection --dir=-

第一个命令将使用mongo创建目标集合.鉴于您的用例,这是必需的,因为根据我刚刚进行的测试,mongorestore不会创建空集合.之后,这只是一个转储/还原过程.

The first command will use mongo to create the destination collection. This is required given your use case as, from what I've just tested, mongorestore will not create an empty collection. After that, this is just a dump/restore process.

请注意,mongodump将接受 --query 参数,以防您需要集合的 partial 副本.

Please note that mongodump will accept a --query parameter in case you need a partial copy of your collection.

这篇关于在mongodb中创建空集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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