Java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id]

After another SO poster (Vinicius Miana) resolved my problem , insert List[DBObject]...

// Bulk insert all documents
collection.insert(MongoDBList(docs)) // docs is List[DBObject]

Now I see this error when trying to insert.

java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id]

EDIT

Full stack trace

[info]   java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id]
[info]   at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:161)
[info]   at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:152)
[info]   at org.bson.types.BasicBSONList.get(BasicBSONList.java:104)
[info]   at com.mongodb.DBCollection.apply(DBCollection.java:767)
[info]   at com.mongodb.DBCollection.apply(DBCollection.java:756)
[info]   at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:220)
[info]   at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:204)
[info]   at com.mongodb.DBCollection.insert(DBCollection.java:76)
[info]   at com.mongodb.casbah.MongoCollectionBase$class.insert(MongoCollection.scala:508)
[info]   at com.mongodb.casbah.MongoCollection.insert(MongoCollection.scala:866)

I checked the post with my same problem, but I'm not sure how to apply the accepted answer.

Does this error mean that I cannot insert key-value pairs in such a way that it is valuenot hidden before Int(behind BasicBSONList )?

+4
source share
3 answers

MongoDBList , BasicDBList, vargs .

List[DBObject], vargs:

val docs = List[DBObject("a" -> "b")
collection.insert(docs: _*)
+1

MongoDBList . , List[DBObject] insert:

collection.insert(docs: _*)

_*, insert - varargs.

, , , . , , MongoDBList , . _id , , .

+3

. Ghik : , MongoDBList. , , , . , , .

I would recommend this approach is not needed! Here is what you need to do. Get a job SalatDAOor ModelCompanion- see https://github.com/novus/salat/wiki/SalatDAO and https://github.com/novus/salat/wiki/SalatWithPlay2 .

Here's an example implementation SalatDAOfor a model object named MyObject:

object MyObjectDAO extends SalatDAO[MyObject, ObjectId](collection = MongoConnection()("my_test_db")("my_test_coll"))

Then just insert your documents with SalatDAO#insert(docs: Traversable[ObjectType], wc: WriteConcern = defaultWriteConcern)

MyObjectDAO.insert(docs)
+2
source

All Articles