【问题标题】:How to combine two collections in mongodb?如何在mongodb中组合两个集合?
【发布时间】:2017-11-10 06:33:47
【问题描述】:

我想合并两个集合(“消息”和“日期”)。 集合“消息”包含这样的文档:

{ 
    "_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", 
}

集合“日期”包含这样的文档:

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

应将日、月和年插入收集消息中。我尝试了不同的方法,但没有找到任何解决方案。

我尝试过的一个例子:

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

        }
    }
])

【问题讨论】:

  • 不要让我们猜测,向我们展示这些尝试!
  • 您正在匹配“2000 年 11 月 14 日星期二 08:22:00 -0800 (PST)”和“2000”

标签: javascript mongodb


【解决方案1】:

用户@Vanojx 在他的评论中提出的问题,

您正在匹配“2000 年 11 月 14 日星期二 08:22:00 -0800 (PST)”与“2000”

但是,为什么消息集合中的日期字段是 RFC 格式并存储为字符串?它应该以 ISO 格式存储为 Date 对象。

根据SO post,聚合框架不能在 v8 引擎中运行,并且您将没有 JS 函数来对字符串进行操作。

除了操作字符串以从中提取“2000”之外,稍后用于加入“日期”集合对我来说听起来不是正确的方法。

如果可能,请将其从字符串转换为日期对象。要将日期字段转换为 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);
});

将“日期”字段从字符串转换为日期对象后,以下查询应该适合您。

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"
        }
    }
    ])

这里我们首先使用$year 从日期字段中提取年份。然后我们将在该字段上使用$lookup 加入。

我希望这会有所帮助。

【讨论】:

  • 我尝试按照您的建议转换日期。但随后出现错误消息,BSON 字符串无法转换为日期。
  • 经过一些实验,您好。我发现您会收到该错误消息的唯一原因是,如果仍然存在某些记录,我给出的脚本无法将“日期”字段从字符串转换为日期数据类型。我尝试使用像“A STRING”这样的无效字符串,它仍然转换为日期数据类型,不用说无效的日期。查询工作正常。看看这个,stackoverflow.com/questions/28415995/…,所以这个查询 db.messages.find({Date: {$not: {$type: 9}}}) 可能会帮助你找到这样的文档。
猜你喜欢
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
  • 2021-08-08
  • 2019-01-28
  • 2017-08-10
  • 2020-09-03
相关资源
最近更新 更多