pymongo - How to keep appending subdocuments in MongoDB? -
i trying bulk insert in mongodb using pymongo. have millions of product/review documents insert mongodb. here structure of document:
{ "_id" : objectid("553858a14483e94d1e563ce9"), "product_id" : "b000gikz4w", "product_category" : "arts", "product_brand" : "unknown", "reviews" : [ { "date" : isodate("2012-01-09t00:00:00z"), "score" : 3, "user_id" : "a3dla3s8qklbnw", "sentiment" : 0.2517857142857143, "text" : "the ink pretty dried upon arrival. was...", "user_gender" : "male", "voted_total" : 0, "voted_helpful" : 0, "user_name" : "womans_roar \"rohrra\"", "summary" : "cute stamps came dried ink" } ], "product_price" : "9.43", "product_title" : "melissa & doug deluxe wooden happy handle stamp set" }
there can multiple reviews single product. requirement insert 1 document per product_id , keep appending more reviews subdocument in reviews array. can please provide pointers on how can achieved? also, nice implement bulk insert performance.
will nice implement bulk insert performance.
in pymongo can execute ordered bulk write operations or unordered bulk write operations
the requirement insert 1 document per product_id , keep appending more reviews subdocument in reviews array
you can use update_one
or update_many
(pymongo 3 or newer) or update
method $push
subdocument reviews
array
collection.update_one({"_id": <doc_id>}, {"$push": {"reviews": <subdocument>}})
or
collection.update({"_id": <doc_id>}, {"$push": {"reviews": <subdocument>}})
to insert need document if no document matches given criteria use upsert
option
collection.update({"_id": <doc_id>}, {"$push": {"reviews": <subdocument>}}, upsert=true)