Edward
Edward

Reputation: 4631

Is it possible to insert several documents to a MongoDB-collection with a single insert() call?

I defined 2 variables in the interactive shell of MongoDB:

a = {"letter": "a"}
b = {"letter": "b"}

Now I'd like to add them to a collection newColl in the database stackoverflow:

use stackoverflow
db.newColl.insert(a)
db.newColl.insert(b)

I was wondering, if it is possible to insert both documents in one go, for example like:

// Not working!
db.newColl.insert(a, b)

Upvotes: 1

Views: 126

Answers (2)

gizq
gizq

Reputation: 197

Just use arrays:

db.collection.isert([doc1:adsadad, doc2:asdad, etc, etc])

http://docs.mongodb.org/manual/reference/method/db.collection.insert/

Upvotes: 1

JohnnyHK
JohnnyHK

Reputation: 311835

If you put both docs into an array it will work:

db.newColl.insert([a, b])

Upvotes: 2

Related Questions