如何使用Mongodb(pymongo)查找密钥

wwtsj6pe  于 2022-12-12  发布在  Go
关注(0)|答案(1)|浏览(133)

I am having a problem to find a particular value of a key with pymongo. below I printed two document of a collection

pprint(list(papers.find().limit(2)))

[{'_id': ObjectId('5fa9a4db76fdd8d66273c643'),
'abstract': '  A fully differential calculation in perturbative quantum '
          'chromodynamics is\n'
          'presented for the production of massive photon pairs at hadron '
          'colliders. All\n'
          'next-to-leading order perturbative contributions from '
          'quark-antiquark,\n'
          'gluon-(anti)quark, and gluon-gluon subprocesses are included, '
          'as well as\n'
          'all-orders resummation of initial-state gluon radiation valid '
          'at\n'
          'next-to-next-to-leading logarithmic accuracy. The region of '
          'phase space is\n'
          'specified in which the calculation is most reliable. Good '
          'agreement is\n'
          'demonstrated with data from the Fermilab Tevatron, and '
          'predictions are made for\n'
          'more detailed tests with CDF and DO data. Predictions are shown '
          'for\n'
          'distributions of diphoton pairs produced at the energy of the '
          'Large Hadron\n'
          'Collider (LHC). Distributions of the diphoton pairs from the '
          'decay of a Higgs\n'
          'boson are contrasted with those produced from QCD processes at '
          'the LHC, showing\n'
          'that enhanced sensitivity to the signal can be obtained with '
          'judicious\n'
          'selection of events.\n',
'authors': "C. Bal\\'azs, E. L. Berger, P. M. Nadolsky, C.-P. Yuan",
'authors_parsed': [['Balázs', 'C.', ''],
                 ['Berger', 'E. L.', ''],
                 ['Nadolsky', 'P. M.', ''],
                 ['Yuan', 'C. -P.', '']],
'categories': 'hep-ph',
'comments': '37 pages, 15 figures; published version',
'doi': '10.1103/PhysRevD.76.013009',
'id': '0704.0001',
'journal-ref': 'Phys.Rev.D76:013009,2007',
'license': None,
'report-no': 'ANL-HEP-PR-07-12',
'submitter': 'Pavel Nadolsky',
'title': 'Calculation of prompt diphoton production cross sections at '
       'Tevatron and\n'
       '  LHC energies',
'update_date': '2008-11-26',
'versions': [{'created': 'Mon, 2 Apr 2007 19:18:42 GMT', 'version': 'v1'},
           {'created': 'Tue, 24 Jul 2007 20:10:27 GMT', 'version': 'v2'}]},
{'_id': ObjectId('5fa9a4db76fdd8d66273c644'),
'abstract': '  We describe a new algorithm, the $(k,\\ell)$-pebble game with '
          'colors, and use\n'
          'it obtain a characterization of the family of '
          '$(k,\\ell)$-sparse graphs and\n'
          'algorithmic solutions to a family of problems concerning tree '
          'decompositions of\n'
          'graphs. Special instances of sparse graphs appear in rigidity '
          'theory and have\n'
          'received increased attention in recent years. In particular, '
          'our colored\n'
          'pebbles generalize and strengthen the previous results of Lee '
          'and Streinu and\n'
          'give a new proof of the Tutte-Nash-Williams characterization of '
          'arboricity. We\n'
          'also present a new decomposition that certifies sparsity based '
          'on the\n'
          '$(k,\\ell)$-pebble game with colors. Our work also exposes '
          'connections between\n'
          'pebble game algorithms and previous sparse graph algorithms by '
          'Gabow, Gabow and\n'
          'Westermann and Hendrickson.\n',
'authors': 'Ileana Streinu and Louis Theran',
'authors_parsed': [['Streinu', 'Ileana', ''], ['Theran', 'Louis', '']],
'categories': 'math.CO cs.CG',
'comments': 'To appear in Graphs and Combinatorics',
'doi': None,
'id': '0704.0002',
'journal-ref': None,
'license': 'http://arxiv.org/licenses/nonexclusive-distrib/1.0/',
'report-no': None,
'submitter': 'Louis Theran',
'title': 'Sparsity-certifying Graph Decompositions',
'update_date': '2008-12-13',
'versions': [{'created': 'Sat, 31 Mar 2007 02:26:18 GMT', 'version': 'v1'},
           {'created': 'Sat, 13 Dec 2008 17:26:00 GMT', 'version': 'v2'}]}]

I am trying to get the nombre of articls that are not published by "Damien Chablat". the articles here are the key title and also we have to use the author key.
I just entred the following command but it's not working:

pprint(len(list(papers.find({},{"authors":{$ne: "Damien Chablat"}, '_id':0}))))

the error is :

pprint(len(list(papers.find({},{"authors":{$ne: "Damien Chablat"}, '_id':0}))))
                                           ^
SyntaxError: invalid syntax

please help me.thanks in advance

sg2wtvxw

sg2wtvxw1#

This first request is for searching a string using regex to find a value("Damien Chablat")

pprint(len(list(papers.find({"authors": {"$regex": "Damien Chablat"}}, 
 {"authors": 1,"authors_parsed": 1,"_id":0}))))

this gave 32 a the number of article published by damien
But we need to find out the articls that are not published by Damien so as follow:

papers.count_documents({"authors": {"$not": {"$regex": "Damien 
 Chablat"}}})

this one gave 9968

相关问题