简体   繁体   中英

How do I connect an Android app to MongoDB stitch?

I've been following this tutorial:

https://code.tutsplus.com/tutorials/how-to-use-mongodb-stitch-in-android-apps--cms-31877

But I'm stuck at around steps 5 (Establish a Connection) and 6 (Insert Documents). I'm really new to Android studio and creating android apps, and the code provided really doesn't show me context on where it all needs to be placed.

I understand everything that's going on, but the tutorial seems a bit vague on where the code needs to be placed in order for the connection to be established to the database.

Thank you for your assistance!

this may help. it's from mongodb website (when logged in)...example snippet:

final StitchAppClient client =
        Stitch.initializeDefaultAppClient("STITCH_CLIENT_APP_ID"); // replace STITCH_CLIENT_APP_ID with App ID

final RemoteMongoClient mongoClient =
        client.getServiceClient(RemoteMongoClient.factory, "mongodb1"); // replace mongodb1 with mongodb-atlas

final RemoteMongoCollection<Document> coll =
        mongoClient.getDatabase("<DATABASE>").getCollection("<COLLECTION>"); // replace <DATABASE> with Stitch db and <COLLECTION> with Stitch collection

client.getAuth().loginWithCredential(new AnonymousCredential()).continueWithTask(
        new Continuation<StitchUser, Task<RemoteUpdateResult>>() {

          @Override
          public Task<RemoteUpdateResult> then(@NonNull Task<StitchUser> task) throws Exception {
            if (!task.isSuccessful()) {
              Log.e("STITCH", "Login failed!");
              throw task.getException();
            }

            final Document updateDoc = new Document(
                    "owner_id",
                    task.getResult().getId()
            );

            updateDoc.put("number", 42);
            return coll.updateOne(
                    null, updateDoc, new RemoteUpdateOptions().upsert(true)
            );
          }
        }
).continueWithTask(new Continuation<RemoteUpdateResult, Task<List<Document>>>() {
  @Override
  public Task<List<Document>> then(@NonNull Task<RemoteUpdateResult> task) throws Exception {
    if (!task.isSuccessful()) {
      Log.e("STITCH", "Update failed!");
      throw task.getException();
    }
    List<Document> docs = new ArrayList<>();
    return coll
            .find(new Document("owner_id", client.getAuth().getUser().getId()))
            .limit(100)
            .into(docs);
  }
}).addOnCompleteListener(new OnCompleteListener<List<Document>>() {
  @Override
  public void onComplete(@NonNull Task<List<Document>> task) {
    if (task.isSuccessful()) {
      Log.d("STITCH", "Found docs: " + task.getResult().toString());
      return;
    }
    Log.e("STITCH", "Error: " + task.getException().toString());
    task.getException().printStackTrace();
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM