0

I have a collection called bills in a database.

Each document in the collection is a bill that was passed in congress.

Within each document, there is an array called votingRecord, comprised of objects that are the voting records of each congress member.

I need to be able to sort and find specific keys/values within the votingRecord array, and I can't figure it out.

I tried examples listed here, but my issue is that each document in my collection has the nested array of objects made of the representatives ("votingRecord").

I need to set an initial filter in my query so that I can search the specific votingRecord for the specific bill.

{
    "_id" : ObjectId("5dc209b7af26e560a3204bcc"),
    "bill_id" : "hr676-116",
    "title" : "To reiterate the support of the Congress of the United States for the North Atlantic Treaty Organization, and for other purposes.",
    "sponsorState" : "CA",
    "sponsorParty" : "D",
    "summary" : "NATO Support Act  This bill prohibits the appropriation or use of funds to withdraw the United States from the North Atlantic Treaty Organization.",
    "primarySubject" : "NATO Support Act  This bill prohibits the appropriation or use of funds to withdraw the United States from the North Atlantic Treaty Organization.",
    "introducedDate" : "2019-01-17",
    "latestMAction" : "Received in the Senate.",
    "votingRecord" : [ 
        {
            "person" : 400440,
            "state" : "AK",
            "district" : 0,
            "vote" : "Yea",
            "name" : "Rep. Don Young [R]",
            "party" : "Republican"
        },

       //( five hundred or so of these objects(all reps + senators), ending with)

],
    "latestMActionDate" : "2019-01-23",
    "__v" : 0
}

my MongoDB shell isn't responding to any of the queries I've made.

db.bills.find({"introducedDate": 2019-01-03},{votingRecord: {$elemMatch: {state:"FL"}}})

the above query felt like the closest, because I specified by "introducedDate," but no luck so far.

niranjan harpale
  • 1,387
  • 1
  • 13
  • 19
Leolal
  • 1
  • 1

1 Answers1

0

If you want to filter out the irrelevant entries from votingRecord array, then you can use the query like:

db.bills.find({"introducedDate": "2019-01-17", "votingRecord.state": "FL"},
{
   "votingRecord.$": 1,     //using $ will only project matched objects   
   //..other fileds that you want to project

});

provided, you want the bill on 2019-01-17 date, with the voting record from "FL" state.

But, lets say

you want the bill on 2019-01-03 date, with the voting record from "FL" state and district "0".

using the above query with an additional condition for the district will not help,

as it won't get that, we want the state to be "FL" and district to be 0 in the same object of the vottingRecord, it will act like an OR condition here,

to use it as AND, you need to use $elemMatch operator,

like this.

db.bills.find({"introducedDate": "2019-01-03", "votingRecord": {$elemMatch: {state:"FL", district: 0}}},
{
   "votingRecord.$": 1,     //using $ will only project matched objects   
   //..other fileds that you want to project

});
niranjan harpale
  • 1,387
  • 1
  • 13
  • 19
  • AH ok. Getting closer thank you. I just need to see all the votes by certain states, and I am too green at this to not break apart your answer and only return that. I tried deleting the additional "{"votingRecord.$:1,"}" and it's not giving it back to me. But you've nudged me further so thank you. – Leolal Nov 08 '19 at 08:39