在mongodb上合并两个集合 [英] Merge two collections on mongodb

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

问题描述

我需要将两个集合合并为一个,但是仅当第一个集合中不存在该文档时,才需要放置该文档.我该怎么做?最好的方法是什么? 我看到了一些有关聚合和mapreduce的信息,但是我不确定该使用什么.你能帮我吗?

I need to merge two collections into one, but I need to put the document only if it doesn't exists in the first collection. How can I do it? What's the best way? I saw something about agregation and mapreduce but I am not sure about what to use. Can you help me, please?

推荐答案

我认为没有自动的方法. 假设您有2个要合并的集合,即c1和c2 您可以手动进行合并

I think there is no automatic way for that. Assume u have 2 collection for merge, c1 and c2 You could manually do the merge with

db.c1.find().forEach(function(item) {
    db.c2.insert(item);
    db.c1.remove(item);
});

您应该注意这一点,因为mongo不支持多文档交易

u should be careful with that because mongo does not support multi-document-transaction

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

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