1
{
    "_id": 4545,
    "username": "testt",
    "data": {
        "passwords": [
            {
                "dataID": "175",
                "username": "enc_data",
                "password": "enc_data",
                "any": "enc_data",
                "any1": "enc_data",
                "any3": "enc_data"
            }
        ],
        "fav_list": []
    }
};

so I'm working on a project when I'll store the user encrypted data in a array inside of an object called 'data' so Ican have a nice organized data storing system but the problem here I can't really access the data from that nested object and I want to access the data with the 'dataID' and update it using the same id for each array object... I'm using python btw. thanks for reading.

2
  • Not entirely sure about your problem here, but have you tried accessing "DataID" using for example --- print(data[passwords][dataID]) Commented Apr 24, 2021 at 15:09
  • yeah i need to access the key to insert data in the database the print is not a problem Commented Apr 24, 2021 at 15:15

2 Answers 2

1

The Mongo way of doing this is using arrayFilters, this let's you update an array element by using a query, like so:

db.collection.updateMany(
{},
{
  "$set": {
    "data.passwords.$[element].property_to_set": "new property value"
  }
},
{
  arrayFilters: [
    {
      "element.dataID": dataID
    }
  ],
})

Mongo Playground

And with pymongo:

collection.update_many(
{},
{
  "$set": {
    "data.passwords.$[element].property_to_set": "new property value"
  }
},
"array_filters"=[{"element.dataID": dataID}]
)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. that what i was looking for but can u help me with something else i need to query all the data from an array object too using the field of dataID. the $elemMatch not working on a nested field i get that error when use that code db.COLLECTION.find({_id:4545},{'data.passwords':{$elemMatch:{dataID:'175'}}})
Sure, If you could specify exactly what is the query and output result you want
i want to provide the object _id and then head to the data object and the passwords array cause every time i'll insert into the passwords array it will have that unique dataID inside of it like i showed in the schema so i want to grab that object from the passwords array using the dataID.
You can't use the projection $elemMatch like that, read a similar question here.
i tried this solution but didn't work the way i wanted it just gets everything.. i only want to get one object from an array using the dataID key inside that data object like i want to get the dataID : '175' which will get me the username and password etc for that specific object inside the array.. is this even possible?
|
0

I removeed the [] of the passwords

data1={
    "_id": 4545,
    "username": "testt",
    "data": {
        "passwords": {
            "dataID": "175",
            "username": "enc_data",
            "password": "enc_data",
            "any": "enc_data",
            "any1": "enc_data",
            "any3": "enc_data"
        },
        "fav_list": []
    }
}
res=data1['data']['passwords']['dataID']
print(res)
# 175

# modify existing data
res=data1['data']['passwords']['dataID']=200
print(res)
# 200

# insert new key
res=data1['data']['passwords']['new_var']=6
print(data1['data']['passwords'].keys())
# dict_keys(['dataID', 'username', 'password', 'any', 'any1', 'any3', 'new_var'])



2 Comments

ok this is good for access the dataID but what if i want to insert data in the passwords array inside that data object
actually this is good way to handle the data in the python file but i want to update the data in the mongodb too so i used that code and it worked db.COLLECTION.update({_id:4545},{$push:{'data.passwords':{username : 'testnew'}}}) this will insert a new data but u got the idea i'm now trying to fetch data using the same mechanics

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.