无法仅更新MongoDB缝合现有文档的一行(使用Android Studio)

时间:2019-01-17 11:14:50

标签: android mongodb-stitch

仅一行(密码行)不能更新MongoDB缝合现有文档。 这是我的收藏集的样子:

owner_id:“ 5c17d8b1698a5454e37cAS45”

facility_password:“ 123456789”

company_name:“ ABS”

我唯一成功的是用新文档替换整个现有文档:

final StitchAppClient client = Stitch.getDefaultAppClient();
                                 final RemoteMongoClient mongoClient = client.getServiceClient
                                         (RemoteMongoClient.factory, "mongodb-atlas");
                                 String UserPasswordStitch = mSharedPreferences.getString
                                         ("UserPasswordSaved", "NullPassword");
                                 final RemoteMongoCollection<Document> coll = mongoClient
                                         .getDatabase("IronHorse_DB").getCollection("Facilities");

                                 client.getAuth().loginWithCredential(new ServerApiKeyCredential
                                         (UserPasswordStitch)).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 queryDoc = new Document("owner_id", task.getResult().getId());
                                         queryDoc.put("facility_password", oldPass);


                                         final Document updatedDoc = new Document("owner_id", task.getResult().getId());
                                         updatedDoc.put("facility_password", newPassFirst);


                                         return coll.updateOne(queryDoc,updatedDoc);
                                     }
                                 }).addOnCompleteListener(new OnCompleteListener<RemoteUpdateResult>()
                                 {
                                     @Override
                                     public void onComplete(@NonNull Task<RemoteUpdateResult> task)
                                     {

                                         if (task.isSuccessful())
                                         {

                                             showCustomToast(getString(R.string.pass_been_changed_success), R.drawable.toast_custom, Toast.LENGTH_LONG);

                                             //recreate .this activity
                                             Handler handler = new Handler();
                                             handler.postDelayed(new Runnable()
                                             {
                                                 @Override
                                                 public void run()
                                                 {
                                                     SharedPreferences.Editor editor = mSharedPreferences.edit();
                                                     editor.putString("onSavedFacilityPass", newPassFirst);
                                                     editor.commit();
                                                     recreate();
                                                 }
                                             }, 1500);

                                             //return;
                                         }
                                         else
                                         {
                                             task.getException().printStackTrace();
                                             showCustomToast("There was a problem with changing password !", R.drawable.toast_emergency_custom, Toast.LENGTH_LONG);
                                         }
                                     }
                                 });

但是我只是想用新数据填充一个密码行

0 个答案:

没有答案