MongoDb upsert exception invalid BSON field

前端 未结 2 1076
生来不讨喜
生来不讨喜 2021-01-04 04:46

This exception:

Exception in thread \"Thread-1\" java.lang.IllegalArgumentException: Invalid BSON field name id
    at org.bson.AbstractBsonWriter.writeName(         


        
2条回答
  •  没有蜡笔的小新
    2021-01-04 05:20

    updateOne for updating document fields using update operators. You need replaceOne which takes the replacement document.

    _collection.replaceOne(
            trackID,
            track,
            new UpdateOptions().upsert( true ));
    

    More here

    Update Operators: https://docs.mongodb.com/manual/reference/operator/update-field/

    Update One:https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/

    Replace One: https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/

提交回复
热议问题