I tried to insert the following DBObject into MongoDB using Spring Data:
BasicDBObject document = new BasicDBObject();
document.put(\"country\", \"us\");
Another way to do it
Import statements
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
Member Variables
@Autowired MongoTemplate mongoTemplate;
MongoCollection collection;
@PostConstruct
public void init(){
collection = mongoTemplate.getCollection("company");
}
And then, the method
public void insertRawData(){
Document company = new Document(new HashMap()); // If you have to insert a hashMap
// otherwise you can add one-by-one using company.put("foo","bar")
collection.insertOne(company);
}