How to insert multiple documents at once ?

1,912 views
Skip to first unread message

João Jerónimo

unread,
Jul 2, 2010, 10:00:40 AM7/2/10
to mongodb-user
Hi everyone, in SQL I used to make something like "INSERT INTO
table(c1,c2) VALUES ('v1',v2) , ('b5','b2') , ('g4','g2') ;" and so
on, and that allowed me to insert multiple rows per query... It helped
me making my application a lot faster, as something like 150 queries
in loops turned to just 3...

Does MongoDB have an equivalent ? How can I do this ?

Thanks in advance,
JJ

Kristina Chodorow

unread,
Jul 2, 2010, 10:10:36 AM7/2/10
to mongod...@googlegroups.com
Yes, most driver support a batch insert that can insert multiple
documents at once. Which language are you using?

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Eliot Horowitz

unread,
Jul 2, 2010, 10:11:20 AM7/2/10
to mongod...@googlegroups.com
Most drivers have a bulk insert method

On Fri, Jul 2, 2010 at 10:00 AM, João Jerónimo
<joaopint...@gmail.com> wrote:

Kyle Banker

unread,
Jul 2, 2010, 10:11:27 AM7/2/10
to mongod...@googlegroups.com
All the drivers support bulk updates. Which driver are you using? You usually can just pass an array of documents to the insert method.

On Fri, Jul 2, 2010 at 10:00 AM, João Jerónimo <joaopint...@gmail.com> wrote:

Luiz Eduardo Kowalski

unread,
Jul 2, 2010, 11:23:13 AM7/2/10
to mongod...@googlegroups.com
hi there
if you're using java, the DBCollection class has a method named insert, that ask for an array of documents

João Jerónimo

unread,
Jul 3, 2010, 8:47:32 AM7/3/10
to mongodb-user
Thanks for the Hint, I'm using the PHP driver.

João Jerónimo

unread,
Jul 3, 2010, 8:49:17 AM7/3/10
to mongodb-user
Is it this what you're talking about ?
http://www.php.net/manual/en/mongocollection.batchinsert.php

On 2 Jul, 15:11, Kyle Banker <k...@10gen.com> wrote:
> All the drivers support bulk updates. Which driver are you using? You
> usually can just pass an array of documents to the insert method.
>
> On Fri, Jul 2, 2010 at 10:00 AM, João Jerónimo
> <joaopintojeron...@gmail.com>wrote:
>
>
>
> > Hi everyone, in SQL I used to make something like "INSERT INTO
> > table(c1,c2) VALUES ('v1',v2) , ('b5','b2') , ('g4','g2') ;" and so
> > on, and that allowed me to insert multiple rows per query... It helped
> > me making my application a lot faster, as something like 150 queries
> > in loops turned to just 3...
>
> > Does MongoDB have an equivalent ? How can I do this ?
>
> > Thanks in advance,
> > JJ
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "mongodb-user" group.
> > To post to this group, send email to mongod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsubscribe@google groups.com>
> > .

Kristina Chodorow

unread,
Jul 3, 2010, 10:17:35 AM7/3/10
to mongod...@googlegroups.com
Yes, exactly.

> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages