I am trying to make a simple connection to mongodb and insert some data. I am new to android studio and mongodb realms. I found some good and simple documentations which i also followed.
I followed these 2 documentations:
- https://www.mongodb.com/docs/realm/sdk/java/app-services/connect-to-app-services-backend/
-
https://www.codersarts.com/post/connect-android-app-with-mongodb-using-realm
I successfully connected to mongodb but now i get just a Unexpected Error. I don’t know what that means and have already checked some answers in the community but nothing helped.
I also checked if i have my user wrongly formated but i think it is right.
I have really no clue what i can still do so any help is appreciated.
Here is my result i get in the console:
Down here I post all my code for connection to MongoDB and insert document
MongoClient mongoClient;
MongoDatabase mongoDatabase;
MongoCollection<AppUser> mongoCollection;
User user;
App app;
String AppId = "application-key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Realm.init(this);
app = new App(new AppConfiguration.Builder(AppId).build());
app.loginAsync(Credentials.anonymous(), new App.Callback<User>() {
@Override
public void onResult(App.Result<User> result) {
initializeMongoDB();
AppUser appUser = new AppUser(
new ObjectId(),
"test",
"test",
"test@gmail.com",
"test",
22);
mongoCollection.insertOne(appUser).getAsync(task -> {
if (task.isSuccess()) {
Log.v("EXAMPLE", "successfully inserted a document with id: " + task.get().getInsertedId());
} else {
Log.v("Example", "user:" + appUser);
Log.e("EXAMPLE", "failed to insert documents with: " + task.getError().getErrorMessage());
}
});
}
});
}
Than I have a function to call the function initializeMongoDB() where I connect to mongodb
private void initializeMongoDB() {
user = app.currentUser();
mongoClient = user.getMongoClient("mongodb-atlas");
mongoDatabase = mongoClient.getDatabase("database");
CodecRegistry pojoCodecRegistry = fromRegistries(AppConfiguration.DEFAULT_BSON_CODEC_REGISTRY,
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
mongoCollection = mongoDatabase.getCollection("user", AppUser.class).withCodecRegistry(pojoCodecRegistry);
}
I define my user Object in AppUser class like this:
public class AppUser extends RealmObject {
@PrimaryKey
@RealmField("_id")
private ObjectId id;
@Required
private String userName;
@Required
private String password;
@Required
private String email;
@Required
private String gender;
@Required
private Integer dob;
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String name) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String email) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getDob() {
return dob;
}
public void setDob(Integer age) {
this.dob = dob;
}
public AppUser() {
}
public AppUser(ObjectId id, String userName, String password, String email, String gender, Integer dob) {
this.id = id;
this.userName = userName;
this.password = password;
this.email = email;
this.dob = dob;
this.gender = gender;
}
public AppUser(String userName, String password, String email, String gender, Integer dob) {
this.userName = userName;
this.password = password;
this.email = email;
this.dob = dob;
this.gender = gender;
}
@Override
public String toString() {
return "[id=" + id + ", userName=" + userName + ", password=" + password + ", email=" + email + ", dob=" + dob + ", gender=" + gender + "]";
}
}