J
J
JIakki2015-11-28 12:18:35
MongoDB
JIakki, 2015-11-28 12:18:35

Why doesn't mongoose add elements to the array?

There is an entry in the database:

{ 
  "_id" : ObjectId("56596dc1e53bfc8510abe7ee"), 
    "date" : { 
    "year_2015" : { 
      "month_11" : { 
        "moneySource" : [ 
          { "img" : "purse.svg", "balance" : 0, "name" : "wallet" }, 
          { "img" : "bank.svg", "balance" : 0, "name" : "bank" } 
        ]
      } 
    } 
  },
  "__v" : 0 
}

Why doesn't the following query add a new object to the array?
Costs.update({
      "_id" : token.costs_id // 56596dc1e53bfc8510abe7ee
    }, {
      "$push" : {
        ['date.'+ year + '.' + month + '.' + post.block] : { //date.year_2015.month_11.moneySource
          "name" : post.name,
          "img" : post.img,
          "balance": 3333333330,
        }
      }
    }, function (err) {
      if(err) {
        console.log(err)
      }
    })

Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2015-11-28
@lega

token.costs_id // 56596dc1e53bfc8510abe7ee

You need to specify an ObjectId, not a string
Square brackets are not needed, in JS you cannot add the key like this - see the errors that fall out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question