Nothing is returned on search of an existing document on the basis of existing id and value

I have kept bmp file in a collection. Is this the reason?

Hi @sps,
Can you give please more information (share sample of documents and find command)?

One possible scenario I can think of is that maybe you have an _id field with ObjectId type and you search it as a String (however this is only a guess).

Second possible scenario I can think of is that you have an id field with name “_id” and you’re searching for “id”.

Thanks,
Rafael,

Sir,
What should be the find query syntax with _id?
What about the case when I have a pid field with string value. The document is there but the result is nothing.
The specialty of this collection is that a .bmp file is kept in the collection in binary format of size 2 MB. Before this, I have kept text file of 2KB in the collection without any problem.
Other than this it has a string description field containing a timestamp and a string file name field.

May be I am asking very basic questions. Something that I must know…

Regards

The documentation for query is at https://docs.mongodb.com/manual/tutorial/query-documents/

The course free M001 from https://university.mongodb.com/ provides a very nice introduction to queries.

A simple query with _id looks like :

{ "_id" : the_value_you_want }

As for the pid case with string it would look like :

{ "pid" : "27001"}

That would make a difference if you are using GridFS. See https://docs.mongodb.com/manual/core/gridfs/

Surprisingly, both options did not work.
With _id The query, I have used is
mycol.find({"_id" : “60ccdc2dfc661dacc3ed8ee9”})
and with pid the query I have used is
mycol.find({“pid” : “3012331370”})

try this:

mycol.find({"_id": ObjectId("60ccdc2dfc661dacc3ed8ee9")})
1 Like

You do not provide any sample documents from your collection. Not surprisingly, there is no way for us to tell you exactly which query will return something.

_id: ObjectId( 60dfdb25241bc196b73725b6)
pid: “1680547105”
filename: “1680547105.bmp”
file:
Binary(‘Qk22lB8AGAAEADYAAAAoAAAAagMAAFACAAABACAAAAAAAICUHwAAAAAAAAAAAAAAAAAAAAAA////AP///wD///8A////AP///wD/…’, 0)

Since you have

you should try

c.find( { "pid" : "1680547105" } )

tried this and obtained empty result set

Post screenshots of what you are doing. Also include a screenshot that shows how you got the sample document you posted earlier.

I will definitely do it asap.
One thing I have not mentioned before.
Some documents are containing a .txt file of 2KB size containing pulse values (1000 documents)
Other documents are containing ECG file in bmp format of size 2 MB (60 documents)

before posting screenshots, which requires redoing insertion of many documents, let me tell what I have done.

  • Step 1: Generated data in csv file containing some details of patients.
  • Step 2: Generated sensor data in .txt file.
  • Step 3: Used actual ecg files in .bmp format
  • Step 4: Inserted many documents with sensor data .txt files.
  • Step 5: In the same collection, with the same set of details, inserted some ecg .bmp files
  • Step 6: Trying to fetch details for a patient id.
  • Step 7: Due to space limitation, I am keeping very less number of fields. One of this fields contains a data time value as string
  • Step 8: I want to do the same work with more number of documents and find the timing.
  • Step 9: I want do the same work by using joining as well.

Using list and append of python to combine documents from different collections.

I want to measure the time difference of accessing and plot a graph.

What is the mistake I am making…

With one or two collections to perform the same work, I am unable to find an existing record…

The code that I am using for finding is as follows:

import pymongo
import time
import datetime
myclient = pymongo.MongoClient("mongodb+srv://.....@cluster0.uxfb5.mongodb.net/<dbname>?retryWrites=true&w=majority")
mydb = myclient["hcd"]
mycol = mydb["fsen1"]
ts1 = time.time()
print(ts1)
#mydoc=list(mycol.find({"pid" : "3012331370"}))
mydoc=list(mycol.find({"_id" : "60ccdc2dfc661dacc3ed8ee9"}))
#mydoc=list(mycol.find({'$and': [{"pid" : "3012331370"},{"filename":"3012331370.bmp"}]}))
print(mydoc)
ts2 = time.time()
print(ts2)
td= ts2-ts1
print('The difference is approx. %s seconds' % td)

If I use Object_Id() with _id I get the following output:

1625933074.741809
Traceback (most recent call last):
  File "D:\nm21\readmongo3.py", line 18, in <module>
    mydoc=list(mycol.find({"_id" : Object_Id("60ccdc2dfc661dacc3ed8ee9")}))
NameError: name 'Object_Id' is not defined

Note that I have removed few commented lines

Completed the full work again starting from storage of data, everything worked well later.