代码之家  ›  专栏  ›  技术社区  ›  Leon Gaban

mongodb:syntaxerror:属性列表后缺少}

  •  0
  • Leon Gaban  · 技术社区  · 5 年前

    所以我有一个带有mongo设置的基本测试数据库。以下是 users 集合当前看起来像:

    {
        "_id" : ObjectId("5d8e23ed2f8849a57711e48e"),
        "firstName" : "Joe",
        "lastName" : "Black",
        "email" : "joeblack@gmail.com",
        "portfolio" : [ ],
        "watchlist" : [ ],
        "goals" : [ ],
        "badges" : [ ],
        "ranks" : [ ],
        "hasPortfolio" : false,
        "hasWatchlist" : false,
        "hasGoals" : false
    }
    {
        "_id" : ObjectId("5d8e23ed2f8849a57711e48f"),
        "firstName" : "Juan",
        "lastName" : "Gonzales",
        "email" : "juangonzales@outlook.com",
        "portfolio" : [ ],
        "watchlist" : [ ],
        "goals" : [ ],
        "badges" : [ ],
        "ranks" : [ ],
        "hasPortfolio" : false,
        "hasWatchlist" : false,
        "hasGoals" : false
    }
    {
        "_id" : ObjectId("5d8e338fe0562db42af793c9"),
        "firstName" : "Foo",
        "lastName" : "Bar",
        "email" : "foobar@baz.com",
        "portfolio" : [ ],
        "watchlist" : [ ],
        "goals" : [ ],
        "ranks" : [ ],
        "badges" : [
            {
                "name" : "Tester",
                "type" : "bronze",
                "new" : true
            }
        ],
        "hasPortfolio" : false,
        "hasWatchlist" : false,
        "hasGoals" : false
    }
    

    我要做的是更新 portfolio 用户数组 foobar@baz.com . 但我一直有以下错误:

    2019-09-27t13:07:00.590-0500 e查询[js]syntaxerror:missing}在属性列表@(shell):6:8之后

    命令

    db.users.update({email: 'foobar@baz.com'}, {
      $set: {
        portfolio: [
          {
            availableSupply: "1000000"
            currency: "SHIT"
            exchange: "Binance",
            exchange_base: "btc",
            marketCap: 10000,
            name: "ShitCoin",
            percentage: 100,
            price: 0.01,
            position: 1,
            value: 0.01,
            inWatchlist: false
          }
        ]
      }
    })
    

    enter image description here

    2 回复  |  直到 5 年前
        1
  •  2
  •   Yaser Darzi Muhammed Albarmavi    5 年前

    请试试这个

    db.users.update({email: 'foobar@baz.com'}, {
      $set: {
        portfolio: [
          {
            availableSupply: "1000000",
            currency: "SHIT",
            exchange: "Binance",
            exchange_base: "btc",
            marketCap: 10000,
            name: "ShitCoin",
            percentage: 100,
            price: 0.01,
            position: 1,
            value: 0.01,
            inWatchlist: false
          }
        ]
      }
    })
    
        2
  •  1
  •   silencedogood    5 年前

    只需在每个项目后添加逗号:

    portfolio: [
      {
        availableSupply: "1000000", // You missed these
        currency: "SHIT", // You missed these
        exchange: "Binance",
        exchange_base: "btc",
        marketCap: 10000,
        name: "ShitCoin",
        percentage: 100,
        price: 0.01,
        position: 1,
        value: 0.01,
        inWatchlist: false
      }
    ]