0

I'm passing id and customer id fields as parameters to get the document. With my below code I'm only able to fetch only those fields of a document. How do I get entire document with multiple fields as parameter?

Code:

@reviews.route('/<inp_id>/<cat_id>', methods=['GET'])
def index(inp_id, cat_id):
    my_coln = mongo_connection.db.db_name
    document = collection.find_one({'id': inp_id}, {'category.id': cat_id})

Result:

{
  "category": {
    "id": "13"
  }, 
  "_id": "5cdd36cd8a348e81d8995d3b"
}

I want:

{
  "customer": {
    "id": "1", 
    "name": "Kit Data"
  }, 
  "category": {
    "id": "13", 
    "name": "TrainKit"
  }, 
  "review_date": "2019-05-06", 
  "phrases": null, 
.....
}

1 Answer 1

1

Pass all your filters in the first dict, the second one is for projection.

document = collection.find_one({'id': inp_id, 'category.id': cat_id})


Your original query, collection.find_one({'id': inp_id}, {'category.id': cat_id}) means give me only category.id (and nothing else (well, apart from _id which is returned by default)) of a document in which the value of id equals inp_id.

Sign up to request clarification or add additional context in comments.

2 Comments

getting result only for the first document. I'm getting null for other combination of id's even though record is present.
because of mixed data types for the same field I was getting null. It works now

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.