0

So, I have a collection like this:

[{
   "name":String,
   "address":String,
   "hobby":Array of Strings
}...]

Every hour I query an API and get the response like so:

[{
   "name":String,
   "address":String,
   "hobby":Array of Strings
}...]

Exactly the same. What I wish to accomplish is, I want to remove all the values from the collection and add all these new values that I am getting from the API response. I can do, delete and insert, but, its a production app and I have noticed that sometimes deletion fails and sometimes insertion fails. I am not able to find the right query to update the db.

This is exactly what I want:

Update the database with new values (Array of objects) every hour while not giving a single damn about the existing values.

The code is written with mongoose in NodeJS. Would appreciate your help.

3
  • why don't you use one of the available update methods? You can update the content using one of them every hour. Commented May 13, 2022 at 12:22
  • Which query would lead to me updating everything in the db irregardless of existing values? @AbdurrahimAhmadov Commented May 13, 2022 at 12:30
  • if I understood correctly, do you want to update the collection with new data and set the fields as ' ' with the data that are not given? Commented May 13, 2022 at 12:37

1 Answer 1

0

Mongodb has a set of command line tools that you could use for this for example, using mongoimport to import (and drop all existing data)

mongoimport --collection=yourcollection --drop --db=databaseName "connectionURI" filename.json

Executing mongoimport inside code with Javascript/Node.js

Though this still isn't ideal. Does the API you're scraping not have any kind of unique identifier for these elements?

It'd be much better to use that ID to replace the documents directly via code, and then perform a separate query to find all IDs that are not in the database but not in the list you got from the API, then delete them

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.