Home > Software engineering >  Aggregate nested array objects from multiple higher level array objects using pymongo
Aggregate nested array objects from multiple higher level array objects using pymongo

Time:12-08

Basically, I have an array of objects with nested arrays of objects. I want to get the flashcardReversed array objects where front is equal to "2front" for user with userID "user1".

Here is my data:

[
  {
    "_id": "608642db80a36336946620aa",
    "userID": "user1",
    "title": "title2",
    "flashcardReversed": [
      {
        "_id": "608d5b290e635ece6828141X",
        "front": "2front",
        "back": "2back",
        "value": "1",
      },
      {
        "_id": "608t5b290e635ece6828141Y",
        "front": "2frontReversed",
        "back": "2backReversed"
        "value": "2",
      },
      {
        "_id": "608a5b31a3f9806de253726X",
        "front": "2front2",
        "back": "2back2"
        "value": "3",
      },
      {
        "_id": "608a5b31a3f9806de253726Y",
        "front": "2frontReversed2",
        "back": "2backReversed2"
        "value": "4",
      }
    ]
  },
  {
    "_id": "608642db80a36336946620aa",
    "userID": "user1",
    "title": "title3",
    "flashcardReversed": [
      {
        "_id": "608d5b290e635ece6828142X",
        "front": "2front",
        "back": "2back",
        "value": "12",
      },
      {
        "_id": "608t5b290e635ece6828143Y",
        "front": "2frontReversed",
        "back": "2backReversed"
        "value": "21",
      },
      {
        "_id": "608a5b31a3f9806de253727X",
        "front": "2front2",
        "back": "2back2"
        "value": "34",
      },
      {
        "_id": "608a5b31a3f9806de253729Y",
        "front": "2frontReversed2",
        "back": "2backReversed2"
        "value": "42",
      }
    ]
  },
  {
    "_id": "608642db80a36336946620aa",
    "userID": "user2",
    "title": "title4",
    "flashcardReversed": [
      {
        "_id": "608d5b290e635ece6828131X",
        "front": "2front",
        "back": "2back",
        "value": "41",
      },
      {
        "_id": "608t5b290e635ece6828161Y",
        "front": "2frontReversed",
        "back": "2backReversed"
        "value": "54",
      },
      {
        "_id": "608a5b31a3f9806de253526X",
        "front": "2front2",
        "back": "2back2"
        "value": "63",
      },
      {
        "_id": "608a5b31a3f9806de253326Y",
        "front": "2frontReversed2",
        "back": "2backReversed2"
        "value": "29",
      }
    ]
  },
]

There are 2 objects with userID "user1", therefore the output should look like this:

{
    "userID": "user1"
    "flashcardReversed": [
      {
        "_id": "608d5b290e635ece6828141X",
        "front": "2front",
        "back": "2back",
        "value": "1",
      },
      {
        "_id": "608d5b290e635ece6828142X",
        "front": "2front",
        "back": "2back",
        "value": "12",
      },
    ]
}

Is it possible to write the code for this using pymongo?

CodePudding user response:

  • $match
  • $unwind
  • $match
  • $group
db.collection.aggregate([
  {
    "$match": {
      "userID": "user1",
      "flashcardReversed.front": "2front"
    }
  },
  {
    "$unwind": "$flashcardReversed"
  },
  {
    "$match": {
      "flashcardReversed.front": "2front"
    }
  },
  {
    "$group": {
      "_id": "$userID",
      "flashcardReversed": {
        "$push": "$$ROOT.flashcardReversed"
      }
    }
  }
])

mongoplayground

pymongo with fastAPI

@app.get("/card")
async def root():
    db = get_database()
    pipeline = [ 
        { "$match": { "userID": "user1", "flashcardReversed.front": "2front" } }, 
        { "$unwind": "$flashcardReversed" }, 
        { "$match": { "flashcardReversed.front": "2front" } }, 
        { "$group": { "_id": "$userID", "flashcardReversed": { "$push": "$$ROOT.flashcardReversed" } } } 
    ]
    c = list(db['card'].aggregate(pipeline))
    return {"message": c}
  • Related