What is the preferred way to add many fields to all documents in a MongoDB collection?

ChemLynx

I have have a Python application that is iteratively going through every document in a MongoDB (3.0.2) collection (typically between 10K and 1M documents), and adding new fields (probably doubling/tripling the number of fields in the document).

My initial thought was that I would use upsert the entire of the revised documents (using pyMongo) - now I'm questioning that:

  • Given that the revised documents are significantly bigger should I be inserting only the new fields, or just replacing the document?
  • Also, is it better to perform a write to the collection on a document by document basis or in bulk?
GHETTO.CHiLD

this is actually a great question that can be solved a few different ways depending on how you are managing your data.

if you are upserting additional fields does this mean your data is appending additional fields at a later point in time with the only changes being the addition of the additional fields? if so you could set the ttl on your documents so that the old ones drop off over time. keep in mind that if you do this you will want to set an index that sorts your results by descending _id so that the most recent additions are selected before the older ones.

the benefit of this of doing it this way is that your are continually writing data as opposed to seeking and updating data so it is faster.

in regards to upserts vs bulk inserts. bulk inserts are always faster than upserts since bulk upserting requires you to find the original document first.

  • Given that the revised documents are significantly bigger should I be inserting only the new fields, or just replacing the document?
    • you really need to understand your data fully to determine what is best but if only change to the data is additional fields or changes that only need to be considered from that point forward then bulk inserting and setting a ttl on your older data is the better method from the stand point of write operations as opposed to seek, find and update. when using this method you will want to db.document.find_one() as opposed to db.document.find() so that only your current record is returned.
  • Also, is it better to perform a write to the collection on a document by document basis or in bulk?
    • bulk inserts will be faster than inserting each one sequentially.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get All 'documents' from MongoDB 'collection'

分類Dev

What is the preferred way to a add a citation suggestion to python packages?

分類Dev

Upsert many documents in MongoDB and php

分類Dev

Get All documents in a Collection CosmosDb

分類Dev

MongoDB prevent documents whose fields are equal

分類Dev

How to stop insertion of Duplicate documents in a mongodb collection

分類Dev

Get the count of the number of documents in a Collection Mongodb

分類Dev

Fast query and deletion of documents of a large collection in MongoDB

分類Dev

Find all documents in a collection with mongo go driver

分類Dev

How to get all documents from a collection in FaunaDB?

分類Dev

What is the preferred way of stating (irrelevant) parameters when overriding methods?

分類Dev

What's the easiest way to validate a string is however many digits it needs to be and is all integers?

分類Dev

MongoDB : querying documents with two equal fields, $match and $eq

分類Dev

Closing mongodb connection in node.js after inserting many documents

分類Dev

Add extra fields to odoo many2many field

分類Dev

MongoDB: How to read all documents from all collections use expressjs?

分類Dev

Updating all documents in a collection (million+) with Date object

分類Dev

Best way to sum array sizes fields in mongoDB

分類Dev

how to group in mongoDB and return all fields in result

分類Dev

Get all in MongoDB collection using SocialCMS with Breeze

分類Dev

Bootstrap way to add vertical space between the fields

分類Dev

How can I rename a field for all documents in MongoDB?

分類Dev

mongodb/mongoose findMany - find all documents with IDs listed in array

分類Dev

Why does the mongodb aggregate $avg code return all of the documents ungrouped?

分類Dev

MongoDB, finding all documents where property id equals to record id

分類Dev

MongoDB: How To Delete All Records Of A Collection in MongoDB Shell?

分類Dev

How to add many fields with ng-switch (angularjs)

分類Dev

Preferred way of adding spinner to toolbar

分類Dev

Bulk add new field to ALL documents in an elasticsearch index

Related 関連記事

  1. 1

    Get All 'documents' from MongoDB 'collection'

  2. 2

    What is the preferred way to a add a citation suggestion to python packages?

  3. 3

    Upsert many documents in MongoDB and php

  4. 4

    Get All documents in a Collection CosmosDb

  5. 5

    MongoDB prevent documents whose fields are equal

  6. 6

    How to stop insertion of Duplicate documents in a mongodb collection

  7. 7

    Get the count of the number of documents in a Collection Mongodb

  8. 8

    Fast query and deletion of documents of a large collection in MongoDB

  9. 9

    Find all documents in a collection with mongo go driver

  10. 10

    How to get all documents from a collection in FaunaDB?

  11. 11

    What is the preferred way of stating (irrelevant) parameters when overriding methods?

  12. 12

    What's the easiest way to validate a string is however many digits it needs to be and is all integers?

  13. 13

    MongoDB : querying documents with two equal fields, $match and $eq

  14. 14

    Closing mongodb connection in node.js after inserting many documents

  15. 15

    Add extra fields to odoo many2many field

  16. 16

    MongoDB: How to read all documents from all collections use expressjs?

  17. 17

    Updating all documents in a collection (million+) with Date object

  18. 18

    Best way to sum array sizes fields in mongoDB

  19. 19

    how to group in mongoDB and return all fields in result

  20. 20

    Get all in MongoDB collection using SocialCMS with Breeze

  21. 21

    Bootstrap way to add vertical space between the fields

  22. 22

    How can I rename a field for all documents in MongoDB?

  23. 23

    mongodb/mongoose findMany - find all documents with IDs listed in array

  24. 24

    Why does the mongodb aggregate $avg code return all of the documents ungrouped?

  25. 25

    MongoDB, finding all documents where property id equals to record id

  26. 26

    MongoDB: How To Delete All Records Of A Collection in MongoDB Shell?

  27. 27

    How to add many fields with ng-switch (angularjs)

  28. 28

    Preferred way of adding spinner to toolbar

  29. 29

    Bulk add new field to ALL documents in an elasticsearch index

ホットタグ

アーカイブ