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

user3675188

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
user3561036

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why could I not assign a value to an array directly, instead I had to use .push()

From Dev

Why do I get "Could not push back" error when trying to use the IBM Bluemix Document Conversion service?

From Dev

When do I need to use a `New` keyword when creating an array?

From Java

What's difference between use new or not when i create an array?

From Dev

Could I use excel to update data on a table?

From Dev

Can I $addToSet and upsert an array?

From Dev

How do I use findAndModify()? to push an array?

From Dev

when is countdown start push data into array java

From Dev

when is countdown start push data into array java

From Dev

Push all data of Array of Strings into new array javascript

From Dev

When should I use push with force?

From Dev

How to push data to array with position [i]?

From Dev

what is the data type that i want to use when merging Array when i merge String array and Integer Array into merge Array

From Dev

how to upsert new field in a nested array

From Dev

push added object to new array when calling getItems()

From Dev

How to push some data from Json into new array in AngularJS?

From Dev

How to push array of adding new data and edit it using angular?

From Java

Data from API call are stored in a Array but when i try to use that array in a function for further use it shows that array is empty . why?

From Dev

When creating a new data frame, how do I set the key labels to the numbers in a linespace np.array?

From Dev

How to check duplicate and upload the new data when I use PHP to upload the CSV file into MySQL database

From Dev

How could I return array and use it in anoher class from database

From Java

I have a problem when I try to push the data to Algolia

From Dev

When should I define a new array in php?

From Dev

When should I use "new" in scala?

From Dev

Use .call when I instantiate a new class

From Dev

How could I use tinyMCE when editing a SlickGrid column?

From Dev

Use recursion for async loop: How to access push array data?

From Dev

Node request data from API, push data to array. Then run another request using that new array

From Dev

Making new array and push there keys on that new array

Related Related

  1. 1

    Why could I not assign a value to an array directly, instead I had to use .push()

  2. 2

    Why do I get "Could not push back" error when trying to use the IBM Bluemix Document Conversion service?

  3. 3

    When do I need to use a `New` keyword when creating an array?

  4. 4

    What's difference between use new or not when i create an array?

  5. 5

    Could I use excel to update data on a table?

  6. 6

    Can I $addToSet and upsert an array?

  7. 7

    How do I use findAndModify()? to push an array?

  8. 8

    when is countdown start push data into array java

  9. 9

    when is countdown start push data into array java

  10. 10

    Push all data of Array of Strings into new array javascript

  11. 11

    When should I use push with force?

  12. 12

    How to push data to array with position [i]?

  13. 13

    what is the data type that i want to use when merging Array when i merge String array and Integer Array into merge Array

  14. 14

    how to upsert new field in a nested array

  15. 15

    push added object to new array when calling getItems()

  16. 16

    How to push some data from Json into new array in AngularJS?

  17. 17

    How to push array of adding new data and edit it using angular?

  18. 18

    Data from API call are stored in a Array but when i try to use that array in a function for further use it shows that array is empty . why?

  19. 19

    When creating a new data frame, how do I set the key labels to the numbers in a linespace np.array?

  20. 20

    How to check duplicate and upload the new data when I use PHP to upload the CSV file into MySQL database

  21. 21

    How could I return array and use it in anoher class from database

  22. 22

    I have a problem when I try to push the data to Algolia

  23. 23

    When should I define a new array in php?

  24. 24

    When should I use "new" in scala?

  25. 25

    Use .call when I instantiate a new class

  26. 26

    How could I use tinyMCE when editing a SlickGrid column?

  27. 27

    Use recursion for async loop: How to access push array data?

  28. 28

    Node request data from API, push data to array. Then run another request using that new array

  29. 29

    Making new array and push there keys on that new array

HotTag

Archive