How to get the document ID after replicating database - android

I am currently making a database program using couchbase on android. After I replicating the data, how can I get the document ID so I can list down all the things in database.

You need to make use of Queries!
If you would like to retrieve all the documents (or their IDs), you could use All-documents query.
Query query = database.createAllDocumentsQuery();
QueryEnumerator result = query.run();
for (Iterator<QueryRow> it = result; it.hasNext(); ) {
QueryRow row = it.next();
Log.w("MYAPP", "document ID: %s", row.getDocumentId()); // or QueryRow also has a way to get its source document.
}
You could write your own Views as per your need and later query them for results.
Also look at how to deal with document(s) in a database over here. In fact, I recommend going through entire guide.

Related

I want to fetch these ids of different users from firestore(see in image).I tried these code but it is giving me blank list as output.Any solutions?

I have tried this code but it giving me blank list.
Future getDocs() async {
QuerySnapshot querySnapshot = await FirebaseFirestore.instance.collection("users").get();
for (int i = 0; i < querySnapshot.docs.length; i++) {
var a = querySnapshot.docs[i];
print(a.id);
}
}
If you check the bottom right of your screenshots, it says:
The document does not exist, it will not appear in queries or snapshots.
So while there is a document ID, that is only shown in the console so that you can select the subcollections. There is no actual document data for the selected document ID.
In fact, all the document IDs in your users collection are shown in italic, because there are no actual documents for any of them. And as the message says, that means they won't show up in read operations.
The simplest way to get the user documents too, is to actually create those documents when you also add the subcollection. What fields you add to the document doesn't really matter, but I tend to start with a createdAt timestamp.
Alternatively, you can run a collection group query across one of the subcollections and then for each resulting document look up the parent document, so determine the IDs.
Something like:
QuerySnapshot querySnapshot = await
FirebaseFirestore.instance.collectionGroup("user_info").get();
// 👆 collection group query
for (int i = 0; i < querySnapshot.docs.length; i++) {
var doc = querySnapshot.docs[i];
print(doc.id);
print(doc.reference.parent.parent.id); // 👈 print parent document id
}
Given that you may end up reading way more documents here, I'd consider this a workaround though, and typically only use it to generate the missing data once.
Also see:
Firestore DB - documents shown in italics
How to access all documents, my all documents only have sub collection in firestore

dart: get SQL like "%text%" search from firebase database snapshot

i am currently working on a search function in my app. this gets a reference of my firebase database snapshot. im using the firebaseDatabase package for this.
currently im using this to search my database
FirebaseDatabase.instance.reference().child('products').orderByChild('title').equalTo(searchQuery)
the searchQuery variable is the the used string to search the database. this returns the list of products which the titles are equal to searchQuery.
searchQuery = 'hello';
title1 = 'hello';
title2 = 'hello there';
for now, only
searchQuery == title1;
searchQuery != title2;
but i also want this to be returned
searchQuery == title2;
is this possible in the way im using this for now?
You are looking for a contains query, but Firebase doesn't provide that. You can refine your actual query by looking for values that are equalTo or endAt or startAt (these methods are available to you as well). Making three requests is going to be costly, though.
If your database is not large and your data is not sensitive, I suggest fetching the whole data and making a client-side filter as shown here.

How to retrieve data from multiple Parse.com Tables/Classes

I have two tables (Classes):
StudentInformation: with columns RollNumber, address, name, school
StudentMarks : with columns RollNumber, Marks1, Marks2, Marks3
I've been able to save the data from a single form into these two simultaneously, but not getting a clue on how to put a query while retrieving into a listview or any other view something like
'return rows (from both tables together) where roll number = 1234' / 'return rows (from both tables together) where Marks2 > 50'
I'm using Parse.com backend for Android
Kindly help
Thanks
First, the UI aspect of showing in a ListView is provided by ParseQueryAdapter. https://parse.com/docs/android_guide#ui-queryadapter
Regarding the query, I do not think you can join tables in the way you want. Instead, you could create a pointer in StudentMarks to StudentInformation.
Then you can query something like:
ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentMarks");
query.include('studentInformation'); // include the pointer to get StudentInformation
query.whereEqualTo("RollNumber", 1234);
query.whereGreaterThan("Marks2", 50);
... // perform query
In the results StudentInformation will be available like this:
List<ParseObject> objects; // the result from the query
ParseObject studentMark = objects.get(0); // example using first object
ParseObject studentInformation = studentMark.get("studentInformation");
String studentName = studentInformation.get("name");
String studentAddress = studentInformation.get("address");
... // etc
Alternatively you could also store a Relation of StudentMarks on StudentInformation, just to let you know that this is also an option, though I do not feel like it fits your current need as well as the solution presented above.

How to iterate through greendao list?

Please help, i'm kinda newbie when it comes to android app with a database.
In Greendao documentation there's a point of having this code:
List users = usersDao.queryBuilder().orderDesc(Properties.Id).list();
But somehow, it is not documented well on how to exactly get the values of rows from that query builder? or is there even a point where I could only get the first row, or last row from the database?
Thanks in advance for someone who will help.
Entity class are similar to all other classes. So greendao generates properties for all rows and you can access them as you access all others properties form "normal" classes.
To access Id property of UserEntity you can use getter user.getId().
From this query you are getting list of users. You can access it as you are accessing any other list in Java.
To get first user from database you can use code similar to:
List<User> users = usersDao.queryBuilder().orderDesc(...).limit(1).list();
if (users.size() < 1) return null;
return users.get(0);
To get last user I suggest to use similar query with reversed order:
List<User> users = usersDao.queryBuilder().orderAsc(...).limit(1).list();
if (users.size() < 1) return null;
return users.get(0);
If your query returns only one unique entity you can use unique method:
User user = usersDao.queryBuilder().where(...).unique();
You don't need to build these queries all the time. You can build it once, and then reuse it:
private Query<User> mSomeUserQuery;
...
// initialization
mSomeUserQuery = usersDao.queryBuilder().where(...).build();
// usage
User user = mSomeUserQuery.forCurrentThread().unique();
Greendao lists are not different to other lists in Java. You just have to google how to iterate through lists. Result: Ways to iterate over a List in java?
But here is the answer with the example to get the name value from an user:
for(User user : users){
String name = user.getName();
}

GreenDAO QueryBuilder dynamically add conditions

I currently have a list of userIds and I am trying to create a query to get all of those from my DB.
this is what I have in mind, I'm just not that sure that it's possible:
ArrayList<Users> listOfUsers = getCurrentUsers();
// lets assume that by now I have a list of users
QueryBuilder<Users> qb = getUsersDao().queryBuilder();
for(Users usr : listOfUsers) {
qb.where(Properties.userId.eq(usr.getUserId());
}
List result = qb.list();
I haven't seen any documentation about what is the right way of doing this and I want to know if this is the correct way of creating a dynamic query in GreenDAO.
EDIT:
I tried this and the result was a NullPointerException in the line of the declaration on the QueryBuilder
try using the IN query instead, it will run faster + you can cache your Query object.
so lets say you have
List<String> userIds;
you can get the list with:
qb.where(Properties.UserId.in(userIds))
if this is an operation that you do frequently, it is better to cache the Query. to do that, prepare the query as follows for only once:
Query<User> query = qb.where(Properties.UserId.in("?")).build();
then when you need to run it :
query.setParameter(0, userIds);
return query.list();

Categories

Resources