问题
This question regarding to Spring boot, Mongo4.0, Spring-data application.
I have upgraded from MongoDB 3.6.x to MongoDB 4.0 and Spring data 2.0.x to Spring-data 2.1.0 for transactional management while inserting data into multiple documents. But here I face this issue while creating a database, collection and inserting a document into the collection.
Issue
Cannot create namespace sampledb_200.demo in multi-document transaction
Here sampldb_200 is database name and demo is the collection name.
In earlier mongodb3.6.X versions, Spring Data 2.0.x, I was able to do database, collection creation, document insertion simultaneously even database and collection do not exist.
Using Spring Data 2.1.0, MongoDB 4.0, Database, collection creation, and insertion does not happen under one function and database, collection creation to be done explicitly.
回答1:
Spring is embracing a new feature shipped with MongoDB 4.0 that supports multi-document transactions. That feature works only for existing collections, ie, a multi-document transaction cannot include an insert operation that would result in the creation of a new collection. You should create your collections before hand to use this feature.
回答2:
So, if you are using mongoose transaction startTransaction
and after you save some new document, with .save({sessions})
please make sure that you have created the correct collection for this document.
Cause, in that case, Mongo will not create the new collection automatically. Check the session option https://mongoosejs.com/docs/api.html#model_Model-save
回答3:
use this annotation on the top of your modal class
@org.springframework.data.mongodb.core.mapping.Document("DEMO")
来源:https://stackoverflow.com/questions/52585715/cannot-create-namespace-in-multi-document-transactionmongodb-4-0-spring-data-2