How do I get projection$ to work in pymongo?


Naki Colson

I have a query which runs in a mongorepl .

I have some files that look like this...

[{              
        "_id" : ObjectId("5b6b6959828619572d48a9da"),
        "files" : [                                                                                     
                {                                                                         
                        "original_filename" : "test_file1.csv",                                                                                                                                                                                                               
                        "s3_key" : "intake/uploads/3edc78aa-2275-45ad-afcb-23bf4a765591/test_file1.csv"                                                                                                                                                                      
                },                                              
                {                                                                                                                                                                                                                                                             
                        "original_filename" : "test_file2.csv",                                                                                                                                                                                                              
                        "s3_key" : "intake/uploads/3edc78aa-2275-45ad-afcb-23bf4a765591/test_file2.csv"          
                }
        ],
        "created_at" : ISODate("2018-08-08T22:06:17.366Z")                                                         
}]

In the mongorepl I can do the following:

db.uploads.find({"_id": ObjectId("5b6b6959828619572d48a9da"), "files.original_filename": "test_file1.csv"}, {"files.$": 1})

I receive data exactly as I want:

{                                                                                                        
        "_id" : ObjectId("5b6b6959828619572d48a9da"),                                                                        
        "files" : [                                                                                                                                                                                                                                                          
                {                                                                                                            
                        "original_filename" : "test_file2.csv",                                                                                                                                                                                                              
                        "s3_key" : "intake/uploads/3edc78aa-2275-45ad-afcb-23bf4a765591/test_file2.csv"                      
                }                                                                                                                                                                                                                                                            
        ]                                                                                                                    
} 

But after pymongoI do this:

query = {
    '_id': upload_id,
    'files.original_filename': 'test_file1.csv'
}
projection = {'files.$': 1}
result = list(self.uploads.find(query, projection))

The result is wrong:

[{'files': {}, '_id': ObjectId('5b6b6959828619572d48a9da')}]

Why? ! ?

Naki Colson
        query = {
            '_id': upload_id
        }
        projection = {
            'files': {
                '$elemMatch': {
                    'original_filename': original_filename
                }
            }
        }
        result = list(self.uploads.find(query, projection))

I switched to this projection and seemed to get the data I wanted. I'm still wondering why, and if there are other better ways to do this.

Related


How do projection matrices work?

Muhammad Lee I have to write a dissertation on 3D programming for my A-level course. But I have a serious problem understanding the perspective projection matrix and I need to explain the matrix in detail. I've searched many websites and youtube videos on this

How do projection matrices work?

Muhammad Lee I have to write a dissertation on 3D programming for my A-level course. But I have a serious problem understanding the perspective projection matrix and I need to explain the matrix in detail. I've searched many websites and youtube videos on this

How do projection matrices work?

Muhammad Lee I have to write a dissertation on 3D programming for my A-level course. But I have a serious problem understanding the perspective projection matrix and I need to explain the matrix in detail. I've searched many websites and youtube videos on this

How do projection matrices work?

Muhammad Lee I have to write a dissertation on 3D programming for my A-level course. But I have a serious problem understanding the perspective projection matrix and I need to explain the matrix in detail. I've searched many websites and youtube videos on this

How do projection matrices work?

Muhammad Lee I have to write a dissertation on 3D programming for my A-level course. But I have a serious problem understanding the perspective projection matrix and I need to explain the matrix in detail. I've searched many websites and youtube videos on this

How do I use $map in MongoDB $projection

arachnid I have a database with documents like this: { _id: 123, metrics: [{ _id: ObjectID(…), name: 'foo', steps: [1, 2, 3, …], values: [10, 12, 13, …] }, {

How do I use $map in MongoDB $projection

arachnid I have a database with documents like this: { _id: 123, metrics: [{ _id: ObjectID(…), name: 'foo', steps: [1, 2, 3, …], values: [10, 12, 13, …] }, {

How do I get @ParametersAreNonnullByDefault to work?

good: I've made several attempts to get the package annotation @ParametersAreNonnullByDefault to work for me in a maven project without success. Can someone share a link to a minimal/example Maven project? I'm talking about letting the findbugs processor imple

How do I get AssImp to work?

light spark I've been trying to do this for about a week now, and still nothing works. I'm building a .dll (UNDONE Engine .dll) that depends on Assimp to load the model. The .dll compiles and links perfectly, but when I try to use it for some applications I ge

How do I get Mockito to work in androidTest

Isaro How can I mock a class I wrote in a test? Here is my test code: import android.support.test.runner.AndroidJUnit4; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotat

How do I get pyinstaller to work on Ubuntu?

microphone I'm using Ubuntu 16 and have installed Python 3.5, also installed pip3, and installed PyInstaller using pip3. I can't run PyInstaller. Whenever I enter pyinstaller in the terminal, I get the following error? do i have to add the path? If so, what sh

How do I get Xdebug to work on Heroku?

Nino Škopac I just pushed a new deployment to a staging instance on Heroku which failed and now my site has a 500 error. composer.json { "license": "proprietary", "require": { "php": ">=5.5.16", "aws/aws-sdk-php": "2.*", "ext-me

How do I get "Observable.of([]);" to work?

rose cleaner What is the correct expression and import Observable.of([]);. import { of } from 'rxjs';doesn't work for me. Martin As of RxJS 6, you should import all "create" observables directly from it 'rxjs'(assuming path mappings are set up when bundling th

How do I get the click catcher to work?

Excellent SP I'm trying to create a simple click catcher, if you click .image-classjavascript it will get the href from another element with the class name .btnand send it to the destination. Although I keep getting errors on lines 7 and 10 that undefined is n

How do I get AssImp to work?

light spark I've been trying to do this for about a week now, and still nothing works. I'm building a .dll (UNDONE Engine .dll) that depends on Assimp to load the model. The .dll compiles and links perfectly, but when I try to use it for some applications I ge

How do I get my torrents to work?

Aelo-74 I'm still new to Ruby/Rails and need your help to make sure the seed is good before running the command.rails db:seed Here is my seed: require 'faker' puts "Creating 10 fake user heros..." 10.times do user = User.create!( username: Faker::DcComi

How do I get pygame collision to work

Crowley I'm making a copy of a flying bird and can't get the collision mechanism to work. Currently, I'm trying to use, .coliderect()but I'm not 100% sure how. Here are two classes. I want the program to do something, or just do something print('x')when the bi

How do I get jQuery to work with me?

Denislav Karagizov I am making an accordion. Watch a guy on youtube do this and write everything his way and it works on his video but not on my computer. Here's what I do (yes, I do declare the HTML at the beginning, and yes, I have the HTML turned on and off

How do I get Tomboy Sync to work?

Jared Kay So after a fresh install of 12.04, I can't get my Tomboy to sync now. I can't get close to 100 important notes right now because "Notes" has also been removed from "Ubuntu One". When I try to "sync" my notes, I get the error "sync failed". Does anyon

How do I get @ParametersAreNonnullByDefault to work?

good: I've made several attempts to get the package annotation @ParametersAreNonnullByDefault to work for me in a maven project without success. Can someone share a link to a minimal/example Maven project? I'm talking about letting the findbugs processor imple