Stitch Android Local DB Location

80 views
Skip to first unread message

Nat101

unread,
Feb 25, 2019, 8:05:44 PM2/25/19
to mongodb-user
How do I tell MongoDB Mobile Android/Stitch WHERE to store the data?
I tried using StitchAppClientConfiguration 'withDirectory", but it bombs out with cluster exceptions.
Thanks.
nat

Andrew Morrow

unread,
Feb 26, 2019, 10:50:53 AM2/26/19
to mongod...@googlegroups.com, mongodb-st...@googlegroups.com

Hi -

I'm adding the mongodb-stitch-users list to this thread. In the future, that list is more likely to get you a quick response on stitch specific topics.

Thanks,
Andrew


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/3b1807d5-5f4c-45da-bf21-22cdd8ddebff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

douglas....@mongodb.com

unread,
Feb 26, 2019, 2:01:45 PM2/26/19
to mongodb-user
Thanks for reaching out.

Your approach seems correct, but you can encounter some issues if the directory you specify does not exist or you or your app do not have sufficient permissions to write to that directory. This is not an issue with the default location as we use your application's data directory which will always exist and will always be visible to and writable by your application.

Also note that the directory you provide is not the directory that will be used by MongoDB, it is the root under which the MongoDB directories will be created.

Let us know if this helps you solve the issue -- if it does not, we will need some extra information to provide further assistance.

Nat101

unread,
Feb 26, 2019, 3:41:41 PM2/26/19
to mongodb-user
1. True the default location is not a problem. But when UN-installing an app, Android deletes all data from default location.
Anyhow, I use 'normal' sdcard folders for all my data in apps I write.
2. The app has the correct permissions. Furthermore, Stitch actually *creates* the folder [structure] and the sql lite stuff before it crashes.

I am attaching screenshots from:
1. My source code
2. The directories Mongo created.
3. Last but not least, the exception stack trace.

Thanks
nat
StitchX_2019-02-26 15_39_06.jpg
StitchX_2019-02-26 15_25_57.jpg
StitchX_2019-02-26 15_23_54.jpg

tyler...@10gen.com

unread,
Mar 9, 2019, 4:34:31 AM3/9/19
to mongodb-user
Hi, 

I have re-created the error using an SD card and am actively looking into figuring out a solution to this problem. We will be sure to let you know when we are able to build in a fix for this, but we are currently targeting it for 4.2.2. 

Thanks, 
Tyler 

Nat101

unread,
Mar 10, 2019, 12:09:47 PM3/10/19
to mongodb-user
Lookin forward.
Thanks;
nat

tyler...@10gen.com

unread,
Mar 11, 2019, 1:20:42 PM3/11/19
to mongodb-user
Hi Nat, 

I replicated your exact error, but it was just a permissions problem. I was able to configure the local mongo to write its data to a file on a SD card. So I have a few followup questions" 

1. Are you certain you have the right permissions? 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

2. Can you try to just write a simple file to this location? 
3. How are you getting the path to the SD card? I found this to be incredibly confusing with some of Android's documentation. 
4. How are you persisting the information after the app gets uninstalled? My initial testing shows that even when using removable/external storage the files get placed in a folder with the app's package name which gets deleted on app uninstall?  

Thanks, 
Tyler

Below is my code: 

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d("appLog", "getExternalFilesDirs");
    for (File f : getExternalFilesDirs(null)) {
      Log.d("appLog", f.getAbsolutePath());
    }

    // Got the below path from calling getExternalFilesDirs()
    File newFile =  new File("/storage/6E0A-1308/Android/data/com.example.sampletest/files");
    try {
      FileOutputStream newFs = new FileOutputStream(newFile);
      newFs.write("test message".getBytes());
      newFs.close();
      Log.d("appLog", "Successfully wrote test file to SD card");
    } catch (Exception e) {
      Log.e("appLog", "Err: ", e);
    }

    final StitchAppClientConfiguration.Builder stitchAppClientBuilder = new StitchAppClientConfiguration.Builder();
    Log.d("appLog", "Made Configuration Builder");

    final StitchAppClientConfiguration stitchAppClientConfiguration = stitchAppClientBuilder.withDataDirectory("/storage/6E0A-1308/Android/data/com.example.sampletest/files").build();
    Log.d("appLog", "Made Configuration");

  final StitchAppClient stitchAppClient = Stitch.initializeAppClient("stitchdocsexamples-pqwyr", stitchAppClientConfiguration);
    Log.d("appLog", "Made StitchAppClient");

  final MongoClient mongoMobileClient = stitchAppClient.getServiceClient(LocalMongoDbService.clientFactory);
    Log.d("appLog", "Made MongoMobileClient");

  final MongoCollection coll = mongoMobileClient.getDatabase("store").getCollection("items");
    Log.d("appLog", "Made Collection");

  // Test to make sure we can insert / read
    coll.insertOne(new Document().append("hey", "hello"));
    Log.d("appLog", String.format("Found Docs: %s", coll.find().first().toString()));
    Log.d("appLog", String.format("Count of Docs: %d", coll.countDocuments()));
}


Nat101

unread,
Mar 11, 2019, 3:37:50 PM3/11/19
to mongodb-user
1. 100% have all the permissions. As evident by the fact that Mongo is actually writing data to that location. (as shown in attached screenshot of a previous message in this thread.)

2. Ok. I am writing and reading text file to the exact same folder, no problem.

3. True, it can be confusing, especially with different versions of Android, but what has worked for me thru many versions of Android:
     new File(Environment.getExternalStorageDirectory().getPath() + "/AnyFolderInSDCARD/plusSubFolders");

4. There is no need to persist the data after app uninstall -IF you write the data to a 'normal' external folder (viewable by the user).
    BUT in your example you are writing the data to the Android/data folder! This is THE folder Android/app automatically wipes out! (upon uninstall) .. And is the default folder where Andoid and the app write to even if you don't use any of the File commands.
     Per syntax of #3, the folder name you use will be a new folder in the SDCard root, with normal user read/write permissions.

Hope this helps.
-nat

Jason Whetton

unread,
Apr 29, 2019, 12:09:52 AM4/29/19
to mongodb-user
Hi,

Was there any resolution to this issue? I am seeing the same thing and have the same need to store the data on external storage.

tyler...@10gen.com

unread,
Apr 30, 2019, 11:16:02 AM4/30/19
to mongodb-user
Hi Jason, 

Currently, MongoMobile is not configured to be able to write to data on external storage on Android. Could you please let us know a bit more about your use case and why the default storage location does not meet your requirements so that we can get a better understanding of the problem?

Thanks, 
Tyler

Jason Whetton

unread,
Apr 30, 2019, 2:47:04 PM4/30/19
to mongodb-user
Hi,

Sure. We currently use Couchbase Mobile/Sync Gateway for sync'ing data to our Android app. The initial sync takes a while and until most documents are down the app is not usable which is an issue for our SLA with our customers. For that reason we move the data store to external (though built in to the device) storage, where it will remain even if the app is deleted for some reason.

We are planning to move to Mongo Mobile once the sync comes out of beta and are preparing for it now, but running from external storage is necessary for us.

I'm surprised it's not possible to have database on external storage - the api seems to allow setting a path and it seemed the database was even created correctly but something didn't work with read or write access once it was created. The necessary permissions were set in the manifest and granted to the app.

Thanks!

Tyler Kaye

unread,
May 8, 2019, 10:10:27 AM5/8/19
to mongod...@googlegroups.com
Hi Jason,

This is definitely something that we would like to do, but unfortunately, I cannot provide a timeline on when this will be available. That being said, I am still confused why exactly you need to store your data on an SD card. If you are just worried about keeping your data around aftwer the applicaton is deleted, I would suggest looking into `android:sharedUserId="android.uid.system"` or use the Android FileProvider system. 

Thanks, 
Tyler

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.

For more options, visit https://groups.google.com/d/optout.


--
{ name       : "Tyler Kaye",
  email        : "tyler...@mongodb.com",
  personal   : "tkay...@gmail.com",
  title           : "Software Engineer",
  phone       : "914-582-9330",
  location     : "New York, NY" }
Reply all
Reply to author
Forward
0 new messages