简体   繁体   English

将mongoDB视图的结果发送到另一个集合(INSERT…SELECT from SQL)

[英]Sending the result of a mongoDB view to another collection ( INSERT…SELECT from SQL )

I could create a big query, and turn it into a view. 我可以创建一个大查询,然后将其转换为视图。 Let's call it DBA_VIEW. 我们称之为DBA_VIEW。

db.DBA_VIEW.find()

I'm using noSQLbooster to interact with mongodb and I'm trying to insert the result of this view into another collection. 我正在使用noSQLbooster与mongodb进行交互,并且试图将此视图的结果插入另一个集合中。

I don't want to "right click > export" because I need to do this via noSQLbooster itself. 我不想“右键单击>导出”,因为我需要通过noSQLbooster本身来执行此操作。

I've seen some querie sthat can do the trick, but, as a SQL SERVER dba, I think I can't get the logic behind, let's say, this one below: 我已经看到一些可以实现此功能的querie,但是,作为SQL SERVER dba,我认为我无法理解下面的逻辑:

db.full_set.find({date:"20120105"}).forEach(function(doc){
   db.subset.insert(doc);
});

how can I use such approach to do my taks? 如何使用这种方式来做我的任务?

I just need the result of that view from a collection, to be inserted in another collection, then after this we are going to export the data into a .json file or even a .TXT. 我只需要从一个集合中获取该视图的结果,然后将其插入另一个集合中,然后在此之后,我们将数据导出到一个.json文件甚至一个.TXT文件中。

You can design the query in the following way so that the output is directly inserted in a new collection('subset' in this example) without iterating through the result set. 您可以按照以下方式设计查询,以便将输出直接插入新集合(在此示例中为“子集”),而无需遍历结果集。

db.full_set.aggregate([
    {
        $match:{
            "date":"20120105"
        }
    },
    {
        $out:"subset"
    }
])

If 'full_set' has the following documents: 如果'full_set'具有以下文档:

{
    "_id" : ObjectId("5d423675bd542251420b6b8e"),
    "date" : "20130105",
    "name" : "Mechanic1"
}
{
    "_id" : ObjectId("5d423675bd542251420b6b8f"),
    "date" : "20120105",
    "name" : "Mechanic2"
}
{
    "_id" : ObjectId("5d423675bd542251420b6b90"),
    "date" : "20120105",
    "name" : "Mechanic3"
}

With the above query, the 'subset' collection would be having the following documents: 通过上述查询,“子集”集合将包含以下文档:

{
    "_id" : ObjectId("5d423675bd542251420b6b8f"),
    "date" : "20120105",
    "name" : "Mechanic2"
}
{
    "_id" : ObjectId("5d423675bd542251420b6b90"),
    "date" : "20120105",
    "name" : "Mechanic3"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM