0

My users' collection used to like:

{'_id':'xxx', 'hobbies':['Dance','Ski']}

Now I add "likes" as:

{'_id':'xxx', 'hobbies':{'Dance':5,'Ski':8}}

I want to query users have at least one same hobby, my old query is like:

db.usr.find({'_id':{'$ne':usr['_id']}, 'hobbies':{'$in':usr['hobbies']} })

Now my query is like:

db.usr.find({'_id':{'$ne':usr['_id']}, 
             'hobbies':{'$in':list(usr['hobbies'].keys())} })

I checked out mongodb documents, found nothing to represent 'hobbies' field name, or python's dictionary key. For mongodb, new 'hobbies' represents embed object, the field name is usually definite.

Do I HAVE TO maintain two arrays(in mongodb) or lists(in python)? Isn't there a simple solution?

{'_id':'xxx', 'hobbies':['Dance','Ski'], 'likes':[5,8]}

1 Answer 1

1

Unfotunately, mongodb does not support querying/filtering field names.

Your options are:

  1. do the filtering on client side, after querying in full
  2. keep hobbies names in an array, like you used to, in order to be able to filter on server side
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.