复制集合并将其粘贴到MongoDB上具有不同名称的同一个数据库中

时间:2022-08-03 01:51:39

I have a Database {Databasename: Home} with 2 collections i.e

我有一个数据库{Databasename:Home},有2个集合,即

Home1 and Home2 //Two existing collection

Now I want to copy Home1 Table entirely and paste it on the same database with new name i.e Home3

Is it Possible to do on MongoDB. I am using Mongoose for my project.

是否可以在MongoDB上进行。我正在使用Mongoose进行我的项目。

And one more thing that is it possible to this job through Cron Job on Nodejs so that it will be a fully automated process.

还有一件事是通过Nodej上的Cron Job可以完成这项工作,这样它就是一个完全自动化的过程。

Any help is really Appreciated.

任何帮助都是真正的赞赏。

2 个解决方案

#1


0  

You can achieve it with $out (aggregation pipeline stage).

您可以使用$ out(聚合管道阶段)实现它。

Example query:

示例查询:

db.getCollection('Home1').aggregate([{ $out : "Home3" }]);

#2


0  

You can copy documents from one collection to another, using collection method copyTo()

您可以使用集合方法copyTo()将文档从一个集合复制到另一个集合

Using mongo shell command it would be like: db.yourCollectionName.copyTo("newCollection")

使用mongo shell命令就像:db.yourCollectionName.copyTo(“newCollection”)

And most probably the same syntax is used in mongoose as well

并且很可能在mongoose中也使用相同的语法

#1


0  

You can achieve it with $out (aggregation pipeline stage).

您可以使用$ out(聚合管道阶段)实现它。

Example query:

示例查询:

db.getCollection('Home1').aggregate([{ $out : "Home3" }]);

#2


0  

You can copy documents from one collection to another, using collection method copyTo()

您可以使用集合方法copyTo()将文档从一个集合复制到另一个集合

Using mongo shell command it would be like: db.yourCollectionName.copyTo("newCollection")

使用mongo shell命令就像:db.yourCollectionName.copyTo(“newCollection”)

And most probably the same syntax is used in mongoose as well

并且很可能在mongoose中也使用相同的语法