开发者

Insert embedded document into an existing document in MongoDB

开发者 https://www.devze.com 2023-03-25 08:44 出处:网络
I have a document with a structure like this: Product (root) Document1 (embedded) Document2 ... DocumentN 开发者_JAVA百科Part1(embedded)

I have a document with a structure like this:

Product (root)
   Document1 (embedded)
   Document2
   ...
   DocumentN
  开发者_JAVA百科    Part1  (embedded)
      Part2
      ...
      PartN

I have POCOs mapped to the structure. At one point, user creates a Product, which is the saved to the database immeadetly. The user then creates a document, after which the Product entity is saved again to the database (as I understand, it should do an update as the product entity already exists in db). However, when I create a N-th document inside a product and try to save it, I get an error that I'm trying to upload too much (maximum upload size limit is 16MB).

How can I update the Product document so that I would insert (upload) only the new Document into the Product instead of uploading the whole product to the database again ?

Thanks!


You need to use upserts. You do this via an update and specifying that it should be an upsert. This will allow you to set/update specific properties without specifying the entire object.

Assuming your products have a productId field which you'll use to identify which product to update:

db.products.update( { "productId":1234 }, { $set: { document1: { part1: "foo" } } }, true );

The last param (true) indicates that this is an upsert. Your driver may have a slightly different way of indicating the upsert.

0

精彩评论

暂无评论...
验证码 换一张
取 消