Elena Lanigan
Elena Lanigan

Reputation: 31

Iterate over a nested array and insert new field in Pymongo

New to MongoDB and the documentation doesnt seem to tell me what I'm looking for.

I have a document like this:

{
    "_id" : "5G",
    " dump" : [ 
        {
        "severity" : "B - Major",
        "problemReportId" : "x",
        "groupInCharge" : "test",
        "feature" : null,
        "_id" : "1",
    },
        {
        "severity" : "BM",
        "problemReportId" : "x",
        "groupInCharge" : "test",
        "feature" : null,
        "_id" : "1",
    }, ]
}

Where dump could have any number... [0,1,2....X, Y, Z]

I want to input a new field let's call it duplicate into the dump dictionarys, so it will look like this:

{
    "_id" : "5G",
    " dump" : [ 
        {
        "severity" : "B - Major",
        "problemReportId" : "x",
        "groupInCharge" : "test",
        "feature" : null,
        "_id" : "1",
        "duplicate": "0",

    },
        {
        "severity" : "M",
        "problemReportId" : "y",
        "groupInCharge" : "testX",
        "feature" : null,
        "_id" : "1",
        "duplicate": "0",
    }, ]
}

I have tried the below code, but all it does is replace the array and I cannot figure out how to iterate through the array.

    for issue in issues:
    # Adding a field to tell if issue is a duplicate, 1 = Yes. 0 = No.
        duplicate_value = {'dump' : { 'duplicate': 0}}
        _key = {"_id": project}
        db.dump.update (_key, duplicate_value, upsert=True)

Upvotes: 1

Views: 676

Answers (2)

Ashh
Ashh

Reputation: 46441

You can try following $map aggregation

db.collection.aggregate([
  {
    $project: {
      dump: {
        $map: {
          input: "$dump",
          as: "dp",
          in: {
            "severity": "$$dp.severity",
            "problemReportId": "$$dp.problemReportId",
            "groupInCharge": "$$dp.groupInCharge",
            "feature": "$$dp.feature",
            "_id": "$$dp._id",
            "duplicate": "0"
          }
        }
      }
    }
  }
])

Upvotes: 1

Alexis Facques
Alexis Facques

Reputation: 1873

If you only need to update your field, you should implement it using Mongo's native operators as @Anthony Winzlet suggested you.

However, if for some reasons you really need to parse and update your array within your Python code, I believe using .save() should work better:

    client = MongoClient(host,port)
    collection = client.collection

    for item in collection.find({ ... }):
        // Do anything you want with the item...
        item["dump"]["duplicate"] = 0
        // This will update the item if the dict has a "_id" field, else will insert as a new doc.
        collection.save(item)

Upvotes: 0

Related Questions