【问题标题】:MongoDB 3.2: Create new field in Aggregation Pipeline from Array ElementsMongoDB 3.2:在聚合管道中从数组元素创建新字段
【发布时间】:2018-04-07 01:20:07
【问题描述】:

我正在尝试根据数组字段中的元素在集合中创建一个新字段。我的语法是:

    db.Accidental_Injuries_Clean.aggregate([{$project:
 {Month: {$arrayElemAt: ["$Dateinfo", 0]}},
{Year: {$arrayElemAt: ["$Dateinfo", 2]}}, 
State: 1, Operations: 1, Place: 1, Killed: 1, 
Injured : 1, ObjectId: 1}}, {$out: "Accidental_Injuries_Cleaner"}]);

这是我收藏的文档示例:

     { "_id" : ObjectId("5ac1dff19a91fd53afc379ca"), 
    "State" : "Delaware", "Operations" : "N/A", 
    "Place" : "Newark", "Killed" : 0, "Injured" : 2,
 "Dateinfo" : [ "November", "23,", "2016" ] }

当我尝试给定命令时,我收到错误:

E QUERY [thread1] SyntaxError: invalid property id @(shell):1:93

我是否使用正确的语法来查找数组的属性?我尝试使用“$Dateinfo.0”,但给了我同样的错误。

任何建议都将不胜感激,非常感谢大家的宝贵时间。

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    您的查询中几乎没有语法错误,只需使用以下查询即可获得所需的输出。

    db.Accidental_Injuries_Clean.aggregate([
    {$project:
     {
         Month: {$arrayElemAt: ["$Dateinfo", 0]},
    Year: {$arrayElemAt: ["$Dateinfo", 2]},
    State: 1, Operations: 1, Place: 1, Killed: 1, 
    Injured : 1, ObjectId: 1
    }},
    {$out: "Accidental_Injuries_Cleaner"}])
    

    【讨论】:

    • 糟糕。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 2022-12-09
    • 2018-04-30
    • 1970-01-01
    • 2018-08-06
    • 2020-12-12
    • 2021-11-17
    相关资源
    最近更新 更多