TylerBrock / mongo-hacker

MongoDB Shell Enhancements for Hackers
tylerbrock.github.io/mongo-hacker
MIT License
1.79k stars 235 forks source link

Avoid querytime display for forEach? #85

Closed dandv closed 10 years ago

dandv commented 10 years ago

New to mongo_hacker... is there a way to disable the repeated displays of Inserted 1 record(s) in 0ms for queries like

db.articles.find().forEach(function (item) {item.type = 'article'; db.content.insert(item)})
TylerBrock commented 10 years ago

Hey Dan,

Sure, but you would be better off doing this the much more performant way:

db.content.insert(db.articles.find().toArray());

It should be apparent from the output that you are doing an insert for every item (and round trip to the database) instead of a single bulk insert.

There is also mongodump/mongorestore which should be used for large collections.

Does that make sense? There is a way to disable it by turning off the verbose messages but I'm not sure you want to.

dandv commented 10 years ago

Hi Tyler, the toArray() is indeed faster, but I need to alter the record before inserting it in the other collection (item.type = 'article'). I could run var a = db.articles.find().toArray() then modify a, then db.content.insert(a).

TylerBrock commented 10 years ago

Sure:

var articles = db.articles.find().toArray();
articles.forEach(function(item){ item.type = 'article' });
db.content.insert(articles);

It would be much faster to use the database instead of javascript for this (if you have millions of documents, or something like that):

// copy the data using mongodump/restore or bulk insert, then:
db.content.update(
    { /* all the things */ },
    { "$set": { type: "article" } },
    { multi: true }
)
TylerBrock commented 10 years ago

Closing this out, if you have any other questions, let me know.