Derek
Derek

Reputation: 131

Mongo Ruby Driver #find() on Specified field Values

I need help querying a MongoDB database with their gem and updating the appropriate fields.

EDIT: I'm trying to loop over documents of a Mongo database, pull down the values of specific fields in those documents, and update them later in the script.

Objectives
  1. Query the database for documents where the field partner_id is "partner" and where the field state is "provisioned", and return only the values under the _id and config fields.

    • After this point, I'll be iterating over each document, generating a password, and updating another database.
  2. Update the database with the newly generated password to each documents config field.

I'm at my wit's end as I've seen about half a dozen different way to write the syntax, and the documentation is little help unless I already knew how to do these things. Any assistance would be greatly appreciated!

#!/usr/bin/env ruby

require 'json'
require 'net/http'
require 'mongo'

# Fetch the addons database URI and connect.
db_uri = ENV['DATABASE_URI']
client = Mongo::Client.new(db_uri)

# Connect to the needed collection and pull down each document to be looped over individually.
# **Having trouble getting this to work. The result is just '= []' - don't know what I'm doing wrong.
client[:collection].find("partner_id" => "partner", "state" => "provisioned", :fields => ["_id", "config"]).each {

  # Need something here to pull down the values from each document's '_id' and 'config' fields and assign them to variables.
  user_id =
  user_config =
  user_config = JSON.parse(user_config)

  # ...generating password and updating other database...

  # Convert the Hash of the user's new configuration into JSON, and update the original database with it.
  # Not sure if any of this is correct. When querying to check, the database doesn't seem to be updated.
  user_config = user_config.to_json
  client[:collection].update(
    {"_id" => user_id},
    {'$set' => {
      "config" => user_config
      }
    }
  )
}
end
return

Upvotes: 2

Views: 2636

Answers (1)

mu is too short
mu is too short

Reputation: 434955

You're not finding anything because this:

:fields => ["_id", "config"]

argument to find isn't specifying the fields you want, find just sees that as a third document field to look for. Your documents probably don't have a field called field whose value is an array of those strings so the query silently finds nothing at all.

If you want to limit your query, you need to use projection:

client[:collection].find("partner_id" => "partner", "state" => "provisioned")
                   .projection('_id' => 1, 'config' => 1)
                   .each { |doc| ... }

Then inside the each block the doc will be a Hash so you can say:

user_id     = doc['user_id']
user_config = doc['user_config']

If I'm reading your code right, the user_config should be a Hash already so you probably won't need to parse it yourself.

Upvotes: 4

Related Questions