jgozal jgozal - 14 days ago 10
Node.js Question

overwriting data on Model.insertMany with Mongoose

I have something like this:

Accounts.insertMany(topAccounts)
.then(function (result) {
console.log('Succesfully saved '+ result.length + ' documents... closing connection...')
mongoose.connection.close();
})
.catch(function(err){
console.log('oh shit');
console.log(err);
})


This appends the saved data to my collection. Is there any way to overwrite it instead? Is my best solution to simply delete the collection before saving the data?

Answer

I've been in this situation as well but for seeding initial database data. I found two different solutions:

  1. Drop the collection - model.collection.drop()
    This is much faster than deleting all documents in a large collection. Mongoose will re-create it automatically but I don't think it automatically ensures indexes are created.
    NOTE: When I did this, there seemed to have been a race condition where it didn't drop right away. If I called dropped then insert (if I recall correctly) I ended up with an empty collection. I simply set a 1 second timeout before inserting all my seed documents and that worked fine.

  2. Delete all documents - model.remove()
    This is a very slow operation on a large collection as it has to update indexes every time it deletes a document.


With that said, I don't know how you're using TopAccounts but it seems like you're treating it like a name-value pair where the name is TopAccounts and the value is an array of TopAccounts. Depending on how you use these, you might want to consider storing all the TopAccounts in a single document - and that would even give you the ability to go back in time and see what Top Accounts there were on a given date.

You could store it something like this and then query by date:

{
    "_id": ObjectId("..."),
    "date": Date("2016-11-19"),
    "top_accounts": [
        {... top account #1 ...},
        {... top account #2 ...},
        {... top account #3 ...}
        ...
    ]
}
Comments