如何在导入 JSON 文件 (pymongo) 时更新 mongo 中的文档

发布于 2025-01-18 05:05:44 字数 1151 浏览 2 评论 0原文

我的测试基础很小,有5个文档和4个论点。在JSON文件导入过程中,我想在导入新文件(添加新字段,用新值替换旧值)时更新这些文档。

以前,我能够在导入CSV文件的过程中进行此过程。

CSV文件的代码:

    def update_and_add_with_csv(self, data, key):

        """ The function update all documents in collection databases using csv file
        (add new columns and change old value). Using pandas """

        df = pd.read_csv(data, low_memory=False)
        df = df.to_dict('records')

        key = key

        try:
            startTime = time.time()

            for row in df:
                self.collection.update_one({key: row.get(key)},  {'$set': row}, upsert=True)
            endTime = time.time()
            totalTime = endTime - startTime
            totalTime = str('{:>.3f}'.format(totalTime))

如何使用JSON完成?

这样的JSON文件:

“在此处输入图像描述”

I have a very small test base, with 5 documents and 4 arguments. I want to update these documents when importing a new file (add new fields, replace old values with new ones) during the JSON file import process.

enter image description here

Previously, I was able to do this process in the process of importing a CSV file.

Code for CSV file:

    def update_and_add_with_csv(self, data, key):

        """ The function update all documents in collection databases using csv file
        (add new columns and change old value). Using pandas """

        df = pd.read_csv(data, low_memory=False)
        df = df.to_dict('records')

        key = key

        try:
            startTime = time.time()

            for row in df:
                self.collection.update_one({key: row.get(key)},  {'$set': row}, upsert=True)
            endTime = time.time()
            totalTime = endTime - startTime
            totalTime = str('{:>.3f}'.format(totalTime))

How can this be done with JSON?

JSON file like this:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2025-01-25 05:05:44

我认为最好的方法是不要更新这些文档,而是要替换它们。

我假设您的日期字段可以用作唯一标识符。

def update_and_add_with_json(self, file_path):

    """ The function update all documents in collection databases using JSON file """

    file_data = json.load(open(file_path, "r"))
    start_time = time.time()
    for record in file_data:
    
        replace = self.collection.find_one_and_replace({"date": record["date"]}, record)
    end_time = time.time()
    total_time = end_time - start_time
    total_time = str('{:>.3f}'.format(total_time))
    return total_time

不确定您的JSON文件是如何格式化的,但是如果将其格式化与模式相同的格式,则可以使用该文件,并使动态添加字段变得更容易,并利用Mongodbs结构更少的功能。

I think the best way to do this is to not update these documents but replace them.

I'm assuming your date fields can be used as unique identifiers.

def update_and_add_with_json(self, file_path):

    """ The function update all documents in collection databases using JSON file """

    file_data = json.load(open(file_path, "r"))
    start_time = time.time()
    for record in file_data:
    
        replace = self.collection.find_one_and_replace({"date": record["date"]}, record)
    end_time = time.time()
    total_time = end_time - start_time
    total_time = str('{:>.3f}'.format(total_time))
    return total_time

Not sure how your json file is formatted but if it is formatted the same way as your schema this should work and make it easier to add fields dynamically and take advantage of MongoDBs structure less feature.

荒岛晴空 2025-01-25 05:05:44

是的,确实如此,它的工作原理类似。可能对某人有用

    def update_and_add_with_json(self, data, key):

        """ The function update all documents in collection databases using JSON file """

        with open(data) as file:
            file_data = json.load(file)

        key = key

        try:
            startTime = time.time()

            for row in file_data:
                self.collection.update_one({key: row.get(key)},  {'$set': row}, upsert=True)
            endTime = time.time()
            totalTime = endTime - startTime
            totalTime = str('{:>.3f}'.format(totalTime))

Yes, exactly, it works in a similar way. Might be useful to someone

    def update_and_add_with_json(self, data, key):

        """ The function update all documents in collection databases using JSON file """

        with open(data) as file:
            file_data = json.load(file)

        key = key

        try:
            startTime = time.time()

            for row in file_data:
                self.collection.update_one({key: row.get(key)},  {'$set': row}, upsert=True)
            endTime = time.time()
            totalTime = endTime - startTime
            totalTime = str('{:>.3f}'.format(totalTime))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文