简体   繁体   English

如何将Android应用程序连接到MongoDB针脚?

[英]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 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). 但是我陷入了第5步(建立连接)和第6步(插入文档)的困境。 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. 我真的是Android Studio和创建android应用程序的新手,并且提供的代码确实没有向我显示所有需要放置的位置的上下文。

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: 它来自mongodb网站(登录时)...示例片段:

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();
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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