如何在 MongoDB 中继续追加子文档?

How to keep appending subdocuments in MongoDB?

我正在尝试使用 PyMongo 在 MongoDB 中进行批量插入。 我有数百万 product/review 文档要插入 MongoDB。这是文档的结构:

{
    "_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 was pretty dried up upon arrival. It was...",
            "user_gender" : "male",
            "voted_total" : 0,
            "voted_helpful" : 0,
            "user_name" : "womans_roar \"rohrra\"",
            "summary" : "Cute stamps but came with dried up ink"
        }
    ],
    "product_price" : "9.43",
    "product_title" : "Melissa & Doug Deluxe Wooden Happy Handle Stamp Set"
} 

单个产品可以有多个评论。要求是为每个 product_id 插入一个文档,并继续将更多评论作为子文档附加到评论数组中。您能否就如何实现这一点提供一些指导?此外,为提高性能实施批量插入会很好。

will be nice to do implement bulk insert for performance.

在pymongo中可以执行Ordered bulk write operations or Unordered Bulk Write Operations

The requirement is to insert one document per product_id and keep appending more reviews as subdocument in the reviews array

您可以使用 update_one or update_many (Pymongo 3 or newer) or update method to $push 子文档到 reviews 数组

collection.update_one({"_id": <doc_id>}, {"$push": {"reviews": <subdocument>}})

collection.update({"_id": <doc_id>}, {"$push": {"reviews": <subdocument>}})

如果没有文档符合给定条件,要插入需要的文档,请使用 upsert 选项

collection.update({"_id": <doc_id>}, {"$push": {"reviews": <subdocument>}}, upsert=True)