0

If I have the below code for inserting data in using mongodb, how can I update it's value. I'm confuse with the setting of arguments in update_one/update_many function.

require 'mongo'
require 'json/ext'

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'myDB')
client[:test_table].insert_many([
    {
          :last_date=> Time.now.strftime("%Y%m%d"),
          :morning_time=> "test",  
          :day1_fullDate=> "test"
    }
])

I failed to do the update function. Please help!

aldrien.h
  • 3,437
  • 2
  • 30
  • 52

1 Answers1

0

SOLVED

test_id = "569466ea4050326b3c000002"
client[:test_table].update_one(
  {"_id" => BSON::ObjectId.from_string(test_id)},
    {"$set" =>
        {
          :last_date=> Time.now.strftime("%Y%m%d"),
          :morning_time=> "test123",  
          :day1_fullDate=> "test123"
        }
     }
 )

I found out that the MongoDB syntax needs to change a little bit depends on what programming language you are using.

For example, in my side I'm using ruby and needs a bit adjustments with the code given in Main Documentation (e.g. https://docs.mongodb.org/manual/reference/method/db.collection.update/#update-parameter)

aldrien.h
  • 3,437
  • 2
  • 30
  • 52