Multiple insert into mongodb - only the first collection gets updated

2.1k Views Asked by At

I am trying to update my collections in my mongodb instance hosted on mlab.

I am running the following code:

...
db.collectionOne.insert(someArrayOfJson)
db.collectionTwo.insert(someArrayOfJson)

The first collection gets updated and the second doesn't.

Using the same/different valid Json arrays produce the same outcome. Only the first gets updated.

I have seen this question duplicate document - same collection and I can understand why it wouldn't work. But my problem is across two seperate collections?

When inserting the data manually on mlab the document goes in the second collection fine - so I am lead to believe it allows duplicate data accross seperate collections.

I am new to mongo - am I missing something simple?

Update:

The response is:

22:01:53.224 [main] DEBUG org.mongodb.driver.protocol.insert - Inserting 20 documents into namespace db.collectionTwo on connection [connectionId{localValue:2, serverValue:41122}] to server ds141043.mlab.com:41043
22:01:53.386 [main] DEBUG org.mongodb.driver.protocol.insert - Insert completed
22:01:53.403 [main] DEBUG org.mongodb.driver.protocol.insert - Inserting 20 documents into namespace db.collectionOne on connection [connectionId{localValue:2, serverValue:41122}] to server ds141043.mlab.com:41043
22:01:55.297 [main] DEBUG org.mongodb.driver.protocol.insert - Insert completed

But there is nothing entered into the db for the second dataset.

Update v2:

If I make a call after the two inserts such as:

db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )

The data collections get updated!

2

There are 2 best solutions below

4
On

How did you determine that the second collection did not get updated?

I believe you are simply seeing the difference between NoSQL and SQL databases. A SQL database will guarantee you that a read after a successful write will read the data you just wrote. A NoSQL database does not guarantee that you can immediately read data you just wrote. See this answer for more details.

0
On

what is total data size ???

here is the sample code it works for me

db.collectionOne.insert([{"name1":"John","age1":30,"cars1":[ "Ford", "BMW", "Fiat"]},{"name1":"John","age1":30,"cars1":[ "Ford", "BMW", "Fiat" ]}]); db.collectionTwo.insert([{"name2":"John","age2":30,"cars2":[ "Ford", "BMW", "Fiat"]},{"name2":"John","age2":30,"cars2":[ "Ford", "BMW", "Fiat" ]}])

If data is more the you can use "Mongo Bulk Write Operations" and also you can refer Mongo DB limits and thresholds

https://docs.mongodb.com/manual/reference/limits