0

Continuation of this.

Now that this is working fine, I've run into another issue. I'm now trying to get a value from a field from a specific document [which shares the same id as the user running the command], however when the command is run it A) pulls from the first document, rather than the one that shares the id with the user, and B) only outputs something like this - {'_id': 012345678987654321, 'inv': {'inv1': 'N/A'}}.

My code:

if conn.mydb.mycol.count_documents({ '_id': userID }):
    inventory1 = conn.mydb.mycol.find_one({}, {'inv': {'inv1':1}, '_id' : userID})
    await ctx.send(inventory1)

How could I go about A) pulling it from the correct document and B) setting the inv1 value as a variable to then be output? Any help is once again appreciated.

1 Answers1

0

The first parameter to find_one() is the filter; the second paremeter is the projection (which fields are returned). So a good start is:

inventory1 = conn.mydb.mycol.find_one({'_id' : userID}, {'inv': 1})
Belly Buster
  • 8,224
  • 2
  • 7
  • 20
  • Thanks for the help! This was able to get the correct document instead of just the top one. The only issue I have now is it outputs the entire inventory instead of just the variable in inv1. How could I go about getting just the variable? Apologies for any inconveniences. – Soulless Maniac May 27 '21 at 18:14
  • Just change the filter i.e. `{'inv.inv1': 1}` – Belly Buster May 28 '21 at 07:17