'How to delete and insert with a single command in mongoDB?

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.



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wazbat