user3675188
user3675188

Reputation: 7409

I could not push an new data into array when use upsert

I could not push an new_data into array when use upsert

How could I use upsert to insert/update one document ?

history is an array.

By the way, the gem mongo's data' seems much much less popular than Python or Javascript.

Is gem mongo has less functions than other languages and has more bugs ?

ArgumentError: wrong number of arguments (3 for 1..2)

[64] pry(#<Scoot>)> @db.data_collection.find({_id: data[:_id]}).update_one(
[64] pry(#<Scoot>)*   {data: data,
[64] pry(#<Scoot>)*     from: from_city,
[64] pry(#<Scoot>)*   },
[64] pry(#<Scoot>)*   {:$push => { "history" => new_data }},
[64] pry(#<Scoot>)*   :upsert => true,
[64] pry(#<Scoot>)*   :safe => true
[64] pry(#<Scoot>)* )
ArgumentError: wrong number of arguments (3 for 1..2)

Mongo::Error::OperationFailure: The dollar ($) prefixed field '$push' in '$push' is not valid for storage. (52)

@db.data_collection.find({_id: data[:_id]}).update_one(
  {data: data,
    from: from_city,
    :$push => { "history" => new_data },
    },
  :upsert => true,
  :safe => true
)

UPDATE GEM MONGO 2.0.4

collection.find({_id: data[:_id]}).update_one(
  {
    "$set" => {
      "city" => from_city
    },
    "$push": => { "history" => new_data }
  },
  :upsert => true
)

ERROR

SyntaxError: unexpected tSTRING_DEND, expecting end-of-input
[105] pry(#<Scoot>)> :upsert => true
SyntaxError: unexpected =>, expecting end-of-input
:upsert => true
          ^
[105] pry(#<Scoot>)> )
SyntaxError: unexpected ')', expecting end-of-input

Upvotes: 0

Views: 451

Answers (1)

user3561036
user3561036

Reputation:

Presuming you are using the 2.X driver series here. Mostly the problem is that the :$push syntax is not supported, so you write it differently. Also, :safe is long deprecated wherever you read that from. Plus you use "$set when you want to set specific fields of an Object:

@db.data_collection.find({ _id: data[:_id]}).update_one({
      "$set" => { 
          "data" => data,
          "city" => from_city
      },
      "$push" => { "history" => new_data }
  },
  :upsert => true
) 

See the documentation for update_one

Upvotes: 1

Related Questions