2016-08-05 7 views
0

ループバック+ mongoDBを使用してAPIを開発中です。その中で私は、MongoDBの中に、文書内の配列を更新する必要があります。ループバックアプリケーションを使用してmongoDBの配列を更新する

マイモデル:

{ 
    "userId": "546fgdfgd445", 
    "rrrr": "7", 
    "bbbbb": [ 
    {} 
    ], 
    "id": "57a437789521b58b12124016" 
} 

私は、フォームのクライアント送信されたデータを使用して新しい要素を持つ配列を更新する必要があります。

app.models.vvvvv.update({ userId: '546fgdfgd445' },{ $push: { "transactions": transactionData }}, function(err, res){ 
     if(err){ 
      console.log(err); 
     } else { 
      console.log(res); 
     } 
    }); 

しかし、私はエラーが発生します:

{ name: 'MongoError', 
    message: 'The dollar ($) prefixed field \'$push\' in \'$push\' is not valid for storage.', 
    driver: true, 
    index: 0, 
    code: 52, 
    errmsg: 'The dollar ($) prefixed field \'$push\' in \'$push\' is not valid for storage.' } 

私はMongoDBのバージョン3.2.3を使用しています。

あなたのアイデアをお伝えください。前もって感謝します。 ?あなたはこのような拡張操作有効にする必要がmodel.json

+0

'transactionData'何ですがそれと間違って何かがあるようです。 – Shrabanee

+0

@Shrabanee No.its完璧なものです。オブジェクトです。アテ配列にオブジェクト({})を押し込もうとしています。\ – Subburaj

+0

これをmongo shellで直接実行できますか?あなたが書いた方法を試してみましたが、うまくいきました。 – Shrabanee

答えて

0

"options": {  
    "mongodb": { 
     "collection": "model_collection", 
     "allowExtendedOperators": true 
    } 
    }, 

または

app.models.vvvvv.update({ userId: '546fgdfgd445' }, 
{ $push: { "transactions": transactionData }}, { allowExtendedOperators: true }, function(err, res){ 
      if(err){ 
       console.log(err); 
      } else { 
       console.log(res); 
      } 
     }); 
関連する問題