Pymongo Updating Collections with appending list values

31 views Asked by At

New to learning about mongodb. I've read the quickstart documentation and the W3 stuff already My question is about how to (best) do the following two things:

Case 1:

{"_id":'mySpecifiedID',
"PersonA": ['pasta','pizza','corn'],
"PersonB": ['pizza','hotdog'],
"PersonC": ['tacos']
}

I want to append the element "pie" to the list corresponding to "PersonB" I know I can find() and get the full existing list-value for "PersonB" and append it to that in memory and then update the document. But I was wondering if there was a way to directly put the value, rather than the expensive search and return and append and update way.

Case 2:

{"_id":'mySpecifiedID',
"PersonA": {"2020-02-01":['pasta','pizza','corn'],
           "2020-02-02":['potato','kraut']},
"PersonB": {"2020-02-01":['pizza','hotdog','corn'],
           "2020-02-02":['duck','nuggets']}
}

I want to append to the value corresponding to "PersonB"["2020-02-02"] I'm not sure how these items are accessible/updatable via PyMongo. Is there a way to only input food items that don't already exist in the array? Without reading everything out first?

More generally, coming from a background of relational databases. I'm not sure what best practice is here. Should I treat a document more like a transaction, i.e. a row in a table? Or more like a table, and the collection as a schema?

I know they don't perfectly parallel but just trying to understand

0

There are 0 answers