2

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!

  • 1
    Are you doing this as part of transaction? Do both collections exist? What are unique keys/indexes for both collections? Are you using the same or different variables for array of documents? – Rashad Ibrahimov Oct 25 '18 at 06:03
  • Not part of a transaction. The indexes are different(prior to the call the collections are removed - the insert recreates them). Interestingly if i call db.createcollection with a new name after the second call both collections get updated –  Oct 25 '18 at 08:12
  • I've updated the questions with the information relating to the above. –  Oct 25 '18 at 08:13
  • `let x1 = [{name:'a'}, {name:'b'}]; let x2 = [{name:'c'}, {name:'d'}];` then `db.one.insert(x1); db.two.insert(x2)` running in the shell works perfectly for me with mongo v4 and clean database. Maybe this example helps you to understand your case – Rashad Ibrahimov Oct 25 '18 at 09:02
  • @JohnM does this also happen in a local MongoDB deployment, or only on mLab? – kevinadi Oct 26 '18 at 05:24
  • Looks to be happening just with mlab. Its strange that it requires another query after the inserts to insert the query before. –  Oct 26 '18 at 08:04
  • you mean insert into 'collectionTwo' but 'collectionOne' updated and nothing insert to collectionTwo ? – HbnKing Nov 06 '18 at 10:58
  • @H.King Yes that is correct. but if i make another call after insert collectionTwo then it will work. see edit V2 –  Nov 06 '18 at 11:17

2 Answers2

0

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

mad Man
  • 366
  • 3
  • 7
-1

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.

Old Pro
  • 24,624
  • 7
  • 58
  • 106
  • "How did you determine that the second collection did not get updated?" - I log inot Mlab and check –  Nov 01 '18 at 09:19
  • @JohnM What language is your program written in? You tagged it `groovy` but `groovy` should be using the Java MongoDB driver which uses `insertOne()` and `insertMany()` rather than `insert()`. If you insert the document into 3 collections, what happens? What happens if you use a write concern of majority, journaled? – Old Pro Nov 03 '18 at 02:26
  • Using Gmongo : https://github.com/poiati/gmongo - but thats just a wrapper –  Nov 06 '18 at 11:20
  • @JohnM Gmongo is very old and not supported, and the behavior you are seeing is likely due to some kind of issue with the asynchronous nature of MongoDB writes and probably an incompatibility with the outdated Java driver it is using. By my reading, Gmongo is using version 2.13.0 of the MongoDB Java driver, while the oldest supported version is 2.13.3 and the current version is 3.8.2. I strongly recommend dumping Gmongo and directly using [the latest Java driver](http://mongodb.github.io/mongo-java-driver/). – Old Pro Nov 06 '18 at 18:59