ReactiveMongo 0.12.7 Fails to create new collection

358 views
Skip to first unread message

ne...@vatbox.com

unread,
Oct 15, 2017, 5:30:00 AM10/15/17
to ReactiveMongo - http://reactivemongo.org
When trying to use: 
reactiveMongoApi.database.map(_.collection(collectionName))


Getting:
Error injecting constructor, reactivemongo.api.commands.bson.DefaultBSONCommandError: CommandError[code=26, errmsg=Collection myDb.coll doesn't exist, doc: {
  "ok": NumberDecimal(0.0),
  "errmsg": "Collection myDb.coll doesn'
t exist",
  "
code": 26,
  "
codeName": "NamespaceNotFound"
}]


Scala version: "2.12.3"

Cédric Chantepie

unread,
Oct 15, 2017, 6:16:27 AM10/15/17
to ReactiveMongo - http://reactivemongo.org
I guess the collectionName given is "myDb.coll", which is invalid as there is no `myDb` database (not that as DB is already resolved, there is no need to specify it again in the collection name).

ne...@vatbox.com

unread,
Oct 15, 2017, 6:18:35 AM10/15/17
to ReactiveMongo - http://reactivemongo.org
No, the db exists, the collection is new: Tests running with existing collection don't throw this error. Only tests creating new collections on an existing db causes the error. 

Cédric Chantepie

unread,
Oct 15, 2017, 6:37:49 AM10/15/17
to ReactiveMongo - http://reactivemongo.org
As collection resolution is tested from MongoDB 2.6 to 3.4, unless a reproducer is shared, the hypothesis that collectionName is wrong is the only one. Best regards.

ne...@vatbox.com

unread,
Oct 15, 2017, 1:16:11 PM10/15/17
to ReactiveMongo - http://reactivemongo.org
Hi,
Taking a deeper look, the problem is not with creating a new collection, but with ensuring an index.
When a collection doesn't exists - 0.12.7 throws the exception while 0.12.6 creates the index. The mongo-shell behaves as 0.12.6.
This project re-creates the issue: master branch uses 0.12.6 while 12-7 uses 0.12.7 version. Hope you find it helpful.

Cédric Chantepie

unread,
Oct 15, 2017, 4:30:08 PM10/15/17
to ReactiveMongo - http://reactivemongo.org
The behaviour at driver level is not the same at MongoShell level, as MongoShell function/operation doesn't always corresponds to the MongoDB protocol (commands).

There it is the MongoDB server which is refusing to ensure an index on a not existing collection.

Kailuo Wang

unread,
Jan 9, 2018, 1:39:39 PM1/9/18
to ReactiveMongo - http://reactivemongo.org
I confirm this is a behavior change in creating a new index on a new db.collection between 0.12.6 and 0.12.7.
When you call indexesManager.ensure with a db and collection yet to be created, with everything else remains the same,
0.12.6 will create the new db, collection and index,
0.12.7 fails with an error 26, either DB does not exist or collection does not. 

I don't think of it as a bug, just a behavior change. That bein said, I would appreciate a work around, e.g. how can I use reactivemongo to ensure a new db and collection is created before calling this indexesManager.ensure? This is particularly needed when running tests on CI like travis. 
Thanks in advance. 

Yardena Meymann

unread,
May 1, 2018, 10:55:08 AM5/1/18
to ReactiveMongo - http://reactivemongo.org
You can use something like:
collection.create().recover {
  case CommandError.Code(48) => () //name exists
} flatmap { _ =>  
  collection.indexManager.ensure(...) 
}

Or optimistically:
collection.indexManager.ensure(...).recoverWith {
  case CommandError.Code(26) => //no collection
    collection.create().recover {
      case CommandError.Code(48) => () //name exists
    } flatmap { _ =>  
      collection.indexManager.ensure(...) 
    }
}

Kailuo Wang

unread,
May 1, 2018, 11:23:19 AM5/1/18
to reacti...@googlegroups.com
Thanks Yardena, that's basically the solution I ended up with. 

--
You received this message because you are subscribed to a topic in the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reactivemongo/NsFBbcL71xA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to reactivemong...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ne...@vatbox.com

unread,
May 1, 2018, 3:02:36 PM5/1/18
to ReactiveMongo - http://reactivemongo.org
It turned out that:
1) ensure index was not the problem, but rather the listIndexes() call under ensure function.
2) ensure index is deprecated from mongo 3.0

Easiest solution was to simplify use createIndex instead. 

James Brems

unread,
Oct 1, 2018, 7:09:40 AM10/1/18
to ReactiveMongo - http://reactivemongo.org
Does this issue still exist in reactivemongo 0.16.0 ? 

I just switched to 0.16.0, and I use ensure index also for not existing collections / dbs, and it seems to work quite well.

BR James
Reply all
Reply to author
Forward
0 new messages