简体   繁体   English

如何在mongodb中合并两个集合?

[英]How to combine two collections in mongodb?

I want to combine two collections ("messages" and "date"). 我想合并两个集合(“消息”和“日期”)。 The collection "messages" contains documents like this one: 集合“消息”包含类似这样的文档:

{ 
    "_id" : ObjectId("4f16fc97d1e2d32371003e27"), 
    "body" : "the scrimmage is still up in the air...\n\n\nwebb said that they didnt want to scrimmage...\n\nthe aggies  are scrimmaging each other... (the aggie teams practiced on \nSunday)\n\nwhen I called the aggie captains to see if we could use their field.... they \nsaid that it was tooo smalll for us to use...\n\n\nsounds like bullshit to me... but what can we do....\n\n\nanyway... we will have to do another practice Wed. night....    and I dont' \nknow where we can practice.... any suggestions...\n\n\nalso,  we still need one  more person...", 
    "subFolder" : "notes_inbox", 
    "mailbox" : "bass-e", 
    "filename" : "450.", 
    "X-cc" : "", 
    "From" : "michael.simmons@enron.com", 
    "Subject" : "Re: Plays and other information", 
    "X-Folder" : "\\Eric_Bass_Dec2000\\Notes Folders\\Notes inbox", 
    "Content-Transfer-Encoding" : "7bit", 
    "X-bcc" : "", 
    "To" : "eric.bass@enron.com", 
    "X-Origin" : "Bass-E", 
    "X-FileName" : "ebass.nsf", 
    "X-From" : "Michael Simmons", 
    "Date" : "Tue, 14 Nov 2000 08:22:00 -0800 (PST)", 
    "X-To" : "Eric Bass", 
    "Message-ID" : "<6884142.1075854677416.JavaMail.evans@thyme>", 
    "Content-Type" : "text/plain; charset=us-ascii", 
    "Mime-Version" : "1.0", 
}

The collection "date" contains documents like this one: 集合“日期”包含类似这样的文档:

{ 
    "_id" : ObjectId("4f16fc97d1e2d32371003e27"), 
    "year" : NumberInt(2000), 
    "month" : NumberInt(11), 
    "day" : NumberInt(14)
}

The day, the month and the year should be inserted into the collection messages. 应该将日期,月份和年份插入收集消息中。 I have tried different approaches but have not come to any solution. 我尝试了不同的方法,但没有找到任何解决方案。

An example of what I've tried: 我尝试过的一个例子:

db.messages.aggregate([
    {
        $lookup:
        {
            from: "date",
            localField: "Date",
            foreignField: "year",
            as: "Year"

        }
    }
])

The problem as the user @Vanojx suggested in his comment, 用户@Vanojx在其评论中建议的问题,

you are matching "Tue, 14 Nov 2000 08:22:00 -0800 (PST)" with "2000" 您将“ Tue,14 Nov 2000 08:22:00 -0800(PST)”与“ 2000”匹配

However, why is the Date field in messages collection in RFC format and stored as a string? 但是,为什么消息收集中的“日期”字段采用RFC格式并存储为字符串? It should have been stored as a Date object in ISO format. 它应该已经以ISO格式存储为Date对象。

According to this SO post , aggregation framework doesn't operate in the v8 engine and you won't have JS functions to do manipulation on the string. 根据这篇SO帖子 ,聚合框架无法在v8引擎中运行,并且您将没有JS函数来对字符串进行操作。

Besides manipulating the string to extract "2000" out of it, to be later used to join on the "date" collection doesn't sound like a right approach to me. 除了操纵字符串以从中提取“ 2000”之外,以后再用于加入“ date”集合的听起来对我来说也不是正确的方法。

If possible please convert it from string to Date object. 如果可能,请将其从字符串转换为Date对象。 To convert date field to Date object you could do this, 要将日期字段转换为Date对象,您可以执行以下操作:

//please make back up of your db just incase
db.messages.find({}).forEach(function (doc) {
    doc.Date = new Date(doc.Date);
    db.messages.save(doc);
});

Once you convert your "Date" field from string to a Date Object, following query should work for you. 将“日期”字段从字符串转换为日期对象后,以下查询将为您工作。

db.messages.aggregate([
    {
        $project: {
            body: 1,
            subFolder: 1,
            Date: 1,
            //add other properties as you need
            //you could even do following which will give you all fields of the document
            //document: "$$ROOT"

            year: {$year: "$Date"}
        }        
    },
    {
        $lookup: {
            from: "date",
            localField: "year",
            foreignField: "year",
            as: "Year"
        }
    }
    ])

Here we first extract the year out of the Date field using $year . 在这里,我们首先使用$ year从Date字段中提取年份 Then we will join using the $lookup on that field. 然后,我们将在该字段上使用$ lookup加入。

I hope this helps. 我希望这有帮助。

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

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