mongodb中的数组创建数组

时间:2013-08-13 03:10:26

标签: mongodb mongoid3

Portfolio类有一个名为folders的数组字段。当我用它创建它时:

db.portfolio.insert(
  folders: [
    {"name"=>"Folder 1", 
     "stocks"=>[
        {"name"=>"stock name", 
         "id"=>"stock id1", 
         "qty"=>2},
        {"name"=>"stock name",
         "id"=>"stock id2",
         "qty"=>1}
    ]},
    {"name"=>"Folder 2",
     "stocks"=>[
        {"name"=>"stock name",
         "id"=>"stock id3",
         "qty"=>2},
        {"name"=>"stock name",
         "id"=>"stock id4",
         "qty"=>1}
    ]}
])

我在参数列表(shell)之后得到“SyntaxError:missing”的错误:1“。任何猜测?

1 个答案:

答案 0 :(得分:1)

这将有效:

db.portfolio.insert({
  folders: [
    {"name":"Folder 1", 
     "stocks":[
        {"name":"stock name", 
         "id":"stock id1", 
         "qty":2},
        {"name":"stock name",
         "id":"stock id2",
         "qty":1}
    ]},
    {"name":"Folder 2",
     "stocks":[
        {"name":"stock name",
         "id":"stock id3",
         "qty":2},
        {"name":"stock name",
         "id":"stock id4",
         "qty":1}
    ]}
]})