This is how the json looks like
"Node" : {
"1-5-2018" : {
"10001" : {
"centre" : "centre_1",
"name" : "name_1",
"paidAmt" : "25000"
},
"10002" : {
"centre" : "centre_2",
"name" : "name_2",
"paidAmt" : "25000"
},
"10003" : {
"centre" : "centre_3",
"name" : "name_3",
"paidAmt" : "10000"
}
},
"2-5-2018 : {
"10004" : {
"centre" : "centre_4",
"name" : "name_4",
"paidAmt" : "20000"
}
}
I want to retrieve the values(centre, name and paidAmt) inside the given dates
I've tried using this particular query but it always shows data does not exist
Query query = databaseReference1.orderByKey().startAt(fromDate.getText().toString()).endAt(toDate.getText().toString());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
Log.d(TAG, "Data exists within dates");
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Log.d(TAG , "Data are " + ds.getValue().toString());
}
}
else {
Log.d(TAG, "Data does not exist within dates");
}
}
Please let me know if it's possible to retrieve all the data within dates. In future i'm planning to add multiple data inside different dates and want to retrieve them based on requirement. Maybe monthly or weekly.
You are using timestamp instead of a date it may look like this.
"Node" : {
"1525188857" : {
"10001" : {
"centre" : "centre_1",
"name" : "name_1",
"paidAmt" : "25000"
},
"10002" : {
"centre" : "centre_2",
"name" : "name_2",
"paidAmt" : "25000"
},
"10003" : {
"centre" : "centre_3",
"name" : "name_3",
"paidAmt" : "10000"
}
},
"1525188873" : {
"10004" : {
"centre" : "centre_4",
"name" : "name_4",
"paidAmt" : "20000"
}
}
Related
I'm currently learning about Spring Boot and am undertaking a project where users can make posts, view those posts, etc.
A user's post(s) can be viewed via http://localhost:8080/users/{user_id}/posts and http://localhost:8080/users/{user_id}/posts/{post_id}
As a result I have the following UserPostController
#RestController
#RequestMapping("/users")
public class UserPostController {
#Autowired
private UserPostService postService;
#GetMapping("/{user_id}/posts")
public List<Post> retrieveUserPosts(#PathVariable int user_id) {
return postService.retrieveUserPostList(user_id);
}
#GetMapping("/{user_id}/posts/{post_id}")
public EntityModel<Post> retrieveUserPost(#PathVariable int user_id, #PathVariable int post_id) {
return postService.retrieveUserPost(user_id, post_id);
}
#PostMapping("/{user_id}/posts")
public ResponseEntity<Object> createUserPost(#PathVariable int user_id, #Valid #RequestBody Post post) {
return postService.saveUserPost(user_id, post);
}
}
Every request to the links work correctly. For example a GET request to http://localhost:8080/users/1/posts returns [{"id":1,"description":"This is a post"},{"id":2,"description":"This is another post"}], which is the expected action.
However, for some reason I am able to visit http://localhost:8080/posts which then returns a list of all posts:
{
"_embedded" : {
"posts" : [ {
"description" : "This is a post",
"_links" : {
"self" : {
"href" : "http://localhost:8080/posts/1"
},
"post" : {
"href" : "http://localhost:8080/posts/1"
},
"user" : {
"href" : "http://localhost:8080/posts/1/user"
}
}
}, {
"description" : "This another post",
"_links" : {
"self" : {
"href" : "http://localhost:8080/posts/2"
},
"post" : {
"href" : "http://localhost:8080/posts/2"
},
"user" : {
"href" : "http://localhost:8080/posts/2/user"
}
}
}, {
"description" : "This is yet another post",
"_links" : {
"self" : {
"href" : "http://localhost:8080/posts/3"
},
"post" : {
"href" : "http://localhost:8080/posts/3"
},
"user" : {
"href" : "http://localhost:8080/posts/3/user"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/posts"
},
"profile" : {
"href" : "http://localhost:8080/profile/posts"
}
},
"page" : {
"size" : 20,
"totalElements" : 3,
"totalPages" : 1,
"number" : 0
}
}
Through HATEOAS I am able to also see available links of the format http://localhost:8080/posts/{user_id}/user which I have also not created methods for, but they still exist.
Is there a reason why these unwanted routes exist? If so how do I change this?
Thank you :)
UPDATE1: I still working on it but I can't solve it, maybe restructuring the database? I'll continue thinking but if someone has an idea it will be great to hear
Well I'm trying to get all current users friends and his own posts in a firebase query but I can't get them, always I tryed I only get one friend posts because the others are overwritten. Let me show you how my data base (JSON) is structured and how I want to display the post in the Android project Activity.
UserRef = FirebaseDatabase.getInstance().getReference().child("Users");
PostRef = FirebaseDatabase.getInstance().getReference().child("Post");
...
...
UserRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
if(dataSnapshot.hasChild("Friends")){
for(DataSnapshot ds : dataSnapshot.child("Friends").getChildren()){
friendsID = ds.getKey(); //String with friendsID
DisplayAllUserPosts();
}
}...
...
private void DisplayAllUserPosts() {
Query SortPost = PostRef.orderByChild("uid").equalTo(friendsID);
FirebaseRecyclerOptions<Posts> options=new FirebaseRecyclerOptions.Builder<Posts>().setQuery(SortPost,Posts.class).build(); \\Posts class is just for getting data base Post nodes elements
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Posts, PostsViewHolder>(options) {...
My Firebase Real Time Database is structured like this:
{
"Post" : {
"10-04-201918:32:09" : {
"counter" : 0,
"date" : "10-04-2019",
"fullname" : "Xuban Illarramendi",
"postimage" : "XXXX",
"profileimage" : "XXXX",
"status" : "",
"time" : "18:32:09",
"uid" : "FP1YJnlJBpfU5aSZlSAErLODWZl2"
},
"10-04-201921:10:06" : {
"counter" : 0,
"date" : "10-04-2019",
"fullname" : "Ander Lopez",
"postimage" : "XXXX",
"profileimage" : "XXXX",
"status" : "",
"time" : "21:10:06",
"uid" : "8wXFsw1pBRYXBVtChqY1Kqi14dy1"
}
},
"Users" : {
"8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
"Friends" : {
"FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
"date" : "10-04-2019"
},
"sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
"date" : "10-04-2019"
}
},
"country" : "Spain",
"dob" : "none",
"fullname" : "Ander Lopez",
"gender" : "none",
"profileimage" : "XXXX",
"relationshipstatus" : "none",
"status" : "",
"username" : "anderlopz"
},
"FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
"Friends" : {
"8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
"date" : "10-04-2019"
},
"sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
"date" : "10-04-2019"
}
},
"country" : "Spain",
"dob" : "none",
"fullname" : "Xuban Illarramendi",
"gender" : "none",
"profileimage" : "XXXX",
"relationshipstatus" : "none",
"status" : "",
"username" : "xuban"
},
"sWQGtchFE1fvuSIfPb42ZIWVkbU2" : {
"Friends" : {
"8wXFsw1pBRYXBVtChqY1Kqi14dy1" : {
"date" : "10-04-2019"
},
"FP1YJnlJBpfU5aSZlSAErLODWZl2" : {
"date" : "10-04-2019"
}
},
"country" : "Spain",
"dob" : "none",
"fullname" : "Unai Lopez",
"gender" : "none",
"profileimage" : "XXXX",
"relationshipstatus" : "none",
"status" : "",
"username" : "unailopz"
}
}
}
So my real problem is that he displays only the last friend posts, how can I make a query that contains all my friendsID without overwritting the previous friendsID?
Please find my mapping query below for the filename field.
PUT /articles
{
"settings" : {
"analysis" : {
"analyzer" : {
"filename_search" : {
"tokenizer" : "filename",
"filter" : ["lowercase"]
},
"filename_index" : {
"tokenizer" : "filename",
"filter" : ["lowercase","edge_ngram"]
}
},
"tokenizer" : {
"filename" : {
"pattern" : "[^\\p{L}\\d]+",
"type" : "pattern"
}
},
"filter" : {
"edge_ngram" : {
"side" : "front",
"max_gram" : 50,
"min_gram" : 1,
"type" : "edgeNGram"
}
}
}
},
"mappings" : {
"doc" : {
"properties" : {
"filename" : {
"type" : "text",
"search_analyzer" : "filename_search",
"analyzer" : "filename_index"
}
}
}
}
}
If am trying to query series1333372 doc623258 and am expecting karthik_series1333372_oracle_page_doc623258_v1_en-EU.pdf. But it's giving all the files which is having series1333372, not even checking for doc623258.
Please find my query below
get articles/_search
{
"query" : {
"match" : {
"filename" : "series1333372 doc623258"
}
}
}
I am inserting the following sample documents for testing from Kibana
POST articles/doc/1
{
"filename" : "karthik_series1333372_oracle_page_doc623258_v1_en-EU.pdf"
}
POST articles/doc/2
{
"filename" : "karthik_series1333372_sun_page_doc658_v1_en-EU.pdf"
}
POST articles/doc/3
{
"filename" : "series1333372_oracle_page_doc623_v1_en-US.pdf"
}
POST articles/doc/4
{
"filename" : "Engineering series1333372 valve_page doc6232 v1_en-US.pdf"
}
POST articles/doc/5
{
"filename" : "Machines_series1333372_page_doc62258_v1_en-US.pdf"
}
POST articles/doc/6
{
"filename" : "AIX series1333372 IBM page doc62358 v1_en-EU.pdf"
}
The default operator of match is OR. If you want all your terms to be present change it like this
GET articles/_search
{
"query" : {
"match" : {
"filename" : {
"query": "series1333372 doc623258",
"operator" : "and"
}
}
}
}
I've a database like this
"Node" : {
"Location_1" : {
"ID1" : {
"address" : "address1",
"balance" : "20",
"batch" : "xyz",
.
.
.
"studentID" : "ID1",
"studentName" : "Username",
"total" : "amount"
}
},
"Location_2" : {
"ID2" : {
"address" : "address2",
"balance" : "0",
"batch" : "xyz",
.
.
"studentID" : "ID2",
"studentName" : "username",
"total" : "amount"
}
}
"Location_3" : {
"ID3" : {
"address" : "address3",
"balance" : "0",
"batch" : "xyz",
.
.
"studentID" : "ID3",
"studentName" : "username",
"total" : "amount"
}
}
I'm able to check if the ID1 exists in a particular Location_1 using the code given below, but I'm unable to find if the ID1 exists across different Location_X id's.
String url = "https://firebaseURL/Node/";
url = url + Location_1;
databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(url);
databaseReference.child(ID1).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
Toast.makeText(getApplicationContext(), "ID already present. Enter unique ID!", Toast.LENGTH_SHORT).show();
}
else { // not present, so go ahead and add
}
I tried using a function like this to check if the user is present in all the subNodes.
private boolean checkIfUserExists(String ID) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Location_1");
.
.
String url = "https://firebaseURL/Node/";
databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(url);
for(String s: arrayList){
if(getUserExists())
break;
databaseReference.child(s).child(ID).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
setUserExists(true);
}
}
But always the function getUserExists return false. So I end up adding a new user in different location or updating the user's info in the same location.
Please let me know how to proceed on checking the existence of the userID in all the locationIDs
Fetch messages method
private void fetchMessages() {
rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId)
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Messages messages = dataSnapshot.getValue(Messages.class);
messagesList.add(messages);
messageAdapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
Defining Ids
MessageSenderId = mAuth.getCurrentUser().getPhoneNumber();
MessageRecieverId = getIntent().getStringExtra("visit_user_id");
trying to make a basic chat app
Since only the senders id is called in rootref only the messages i send r being displayed in d recyclerview... im unable to receive messages because of this... how can i make it to retrieve recievers id and senders id also at the same time
Database strucutre
{
"Messages" : {
"+918105571584" : {
"+919945342730" : {
"-L58IPCLEeE21vH_-1Ry" : {
"message" : "Hi",
"seen" : false,
"time" : 1518427022478,
"type" : "text"
},
"-L58IU1VIHN0rHaUox3a" : {
"message" : "Hello",
"seen" : false,
"time" : 1518427042257,
"type" : "text"
},
"-L58IYN1GpHPdkWCY7Hn" : {
"message" : "Hi",
"seen" : false,
"time" : 1518427060021,
"type" : "text"
}
}
},
"+919945342730" : {
"+918105571584" : {
"-L58IPCLEeE21vH_-1Ry" : {
"message" : "Hi",
"seen" : false,
"time" : 1518427022478,
"type" : "text"
},
"-L58IU1VIHN0rHaUox3a" : {
"message" : "Hello",
"seen" : false,
"time" : 1518427042257,
"type" : "text"
},
"-L58IYN1GpHPdkWCY7Hn" : {
"message" : "Hi",
"seen" : false,
"time" : 1518427060021,
"type" : "text"
}
}
}
},
"Users" : {
"+918105571584" : {
"Email" : "",
"Name" : "Akash",
"Quote" : "",
"Status" : ""
},
"+919945342730" : {
"Email" : "",
"Name" : "Sav",
"Quote" : "",
"Status" : ""
}
}
}
According to your last comment, I understand that you can display only the messages that correpond to a single user. A user can see only his messages butd not other user messages. To solve this, you need to create chat rooms, in which every user can add messages that can be seen by all chat room members.
So for that, you need to change your database structure. You cannot achieve this, using the actual database.