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

StackOverflow https://stackoverflow.com/questions/19058990

  •  29-06-2022
  •  | 
  •  

Domanda

After another SO poster(Vinicius Miana) resolved my issue to insert a List[DBObject] ...

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

Now, I'm seeing 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've checked out a post with my exact same problem, but I'm not sure how to apply the accepted answer.

Does this error mean that I cannot insert any key-value pairs such that value is not castable to Int (per BasicBSONList)?

È stato utile?

Soluzione 3

MongoDBList is not the same as an ordinary list, it is a convenience wrapper for BasicDBList and as such converting to vargs doesn't work as expected.

You should supply List[DBObject] and then explode into vargs:

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

Altri suggerimenti

You simply can't insert a MongoDBList into a collection. If you want to insert multiple documents in a single operation, you just need to pass your List[DBObject] directly to insert method:

collection.insert(docs: _*)

The _* is needed because insert is a varargs method.

On the other side I have to admit that this exception is quite confusing here and its error message doesn't have too much to do with the actual problem. I suspect it happens because Casbah tries to treat MongoDBList as a regular document that can be inserted into database. It attempts to access the _id of the object that it tries to insert and that causes the exception.

Salat author here. Ghik is correct: your problem is that you are wrapping your model objects in a spurious MongoDBList. Also, you appear to be feeding model objects directly to your collection. If you're going to do that, you need to manually serialize each object before inserting it.

I would recommend this approach is unnecessary! Here's what you need to do. Get either SalatDAO or ModelCompanion working - see https://github.com/novus/salat/wiki/SalatDAO and https://github.com/novus/salat/wiki/SalatWithPlay2.

Here's a sample implementation of SalatDAO for a model object called MyObject:

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

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

MyObjectDAO.insert(docs)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top