How to update user in the following structure












0















Assuming my user is visiting places in the world, and the database looks like the following, what's the best practice to delete the user from the old place, and have him at the new place (so that this unique user can only be at single place at once)?



At first user is at location A:



database
|
places
|___location A
| |___user1: "exact coordinates"
|
|___location B
|___user2: "exact coordinates"


And this is how I want it to look like after moving user1 to location B:



   database
|
places
|___location B
|___user1: "exact coordinates"
|___user2: "exact coordinates"


How to move user1 to location B while removing him from location A?
I am having hard time finding the correct syntax to simply delete the (uniquely identified) user1 from his old place to his new place.










share|improve this question




















  • 1





    How about using a transaction to execute both CRUD operations sort of "together"? informit.com/articles/article.aspx?p=2731932&seqNum=2

    – giro
    Nov 15 '18 at 11:05













  • But this is firebase database, or I am completely off? (Sorry I am a beginner)

    – TTnote
    Nov 15 '18 at 11:09






  • 1





    Use a separate branch for users and places. users->user_1->{location, coords}. Perhaps the location is list of nearby userids locations->location->{user_id : timestamp}. Throw an onWrite trigger to remove the userid from the old location.

    – James Poag
    Nov 15 '18 at 11:11













  • James, yes this should be a list of nearby userids, how did you know? :D. Do you have a place where I can see how to do that efficiently?

    – TTnote
    Nov 15 '18 at 11:14











  • @JamesPoag, as a beginner, I can't find the relation between the onWrite in the link to the onWrite in Java code for android. How would I use onWrite inside my project in Android Studio?

    – TTnote
    Nov 15 '18 at 11:28
















0















Assuming my user is visiting places in the world, and the database looks like the following, what's the best practice to delete the user from the old place, and have him at the new place (so that this unique user can only be at single place at once)?



At first user is at location A:



database
|
places
|___location A
| |___user1: "exact coordinates"
|
|___location B
|___user2: "exact coordinates"


And this is how I want it to look like after moving user1 to location B:



   database
|
places
|___location B
|___user1: "exact coordinates"
|___user2: "exact coordinates"


How to move user1 to location B while removing him from location A?
I am having hard time finding the correct syntax to simply delete the (uniquely identified) user1 from his old place to his new place.










share|improve this question




















  • 1





    How about using a transaction to execute both CRUD operations sort of "together"? informit.com/articles/article.aspx?p=2731932&seqNum=2

    – giro
    Nov 15 '18 at 11:05













  • But this is firebase database, or I am completely off? (Sorry I am a beginner)

    – TTnote
    Nov 15 '18 at 11:09






  • 1





    Use a separate branch for users and places. users->user_1->{location, coords}. Perhaps the location is list of nearby userids locations->location->{user_id : timestamp}. Throw an onWrite trigger to remove the userid from the old location.

    – James Poag
    Nov 15 '18 at 11:11













  • James, yes this should be a list of nearby userids, how did you know? :D. Do you have a place where I can see how to do that efficiently?

    – TTnote
    Nov 15 '18 at 11:14











  • @JamesPoag, as a beginner, I can't find the relation between the onWrite in the link to the onWrite in Java code for android. How would I use onWrite inside my project in Android Studio?

    – TTnote
    Nov 15 '18 at 11:28














0












0








0








Assuming my user is visiting places in the world, and the database looks like the following, what's the best practice to delete the user from the old place, and have him at the new place (so that this unique user can only be at single place at once)?



At first user is at location A:



database
|
places
|___location A
| |___user1: "exact coordinates"
|
|___location B
|___user2: "exact coordinates"


And this is how I want it to look like after moving user1 to location B:



   database
|
places
|___location B
|___user1: "exact coordinates"
|___user2: "exact coordinates"


How to move user1 to location B while removing him from location A?
I am having hard time finding the correct syntax to simply delete the (uniquely identified) user1 from his old place to his new place.










share|improve this question
















Assuming my user is visiting places in the world, and the database looks like the following, what's the best practice to delete the user from the old place, and have him at the new place (so that this unique user can only be at single place at once)?



At first user is at location A:



database
|
places
|___location A
| |___user1: "exact coordinates"
|
|___location B
|___user2: "exact coordinates"


And this is how I want it to look like after moving user1 to location B:



   database
|
places
|___location B
|___user1: "exact coordinates"
|___user2: "exact coordinates"


How to move user1 to location B while removing him from location A?
I am having hard time finding the correct syntax to simply delete the (uniquely identified) user1 from his old place to his new place.







android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:09









Grimthorr

4,47442236




4,47442236










asked Nov 15 '18 at 11:01









TTnoteTTnote

969




969








  • 1





    How about using a transaction to execute both CRUD operations sort of "together"? informit.com/articles/article.aspx?p=2731932&seqNum=2

    – giro
    Nov 15 '18 at 11:05













  • But this is firebase database, or I am completely off? (Sorry I am a beginner)

    – TTnote
    Nov 15 '18 at 11:09






  • 1





    Use a separate branch for users and places. users->user_1->{location, coords}. Perhaps the location is list of nearby userids locations->location->{user_id : timestamp}. Throw an onWrite trigger to remove the userid from the old location.

    – James Poag
    Nov 15 '18 at 11:11













  • James, yes this should be a list of nearby userids, how did you know? :D. Do you have a place where I can see how to do that efficiently?

    – TTnote
    Nov 15 '18 at 11:14











  • @JamesPoag, as a beginner, I can't find the relation between the onWrite in the link to the onWrite in Java code for android. How would I use onWrite inside my project in Android Studio?

    – TTnote
    Nov 15 '18 at 11:28














  • 1





    How about using a transaction to execute both CRUD operations sort of "together"? informit.com/articles/article.aspx?p=2731932&seqNum=2

    – giro
    Nov 15 '18 at 11:05













  • But this is firebase database, or I am completely off? (Sorry I am a beginner)

    – TTnote
    Nov 15 '18 at 11:09






  • 1





    Use a separate branch for users and places. users->user_1->{location, coords}. Perhaps the location is list of nearby userids locations->location->{user_id : timestamp}. Throw an onWrite trigger to remove the userid from the old location.

    – James Poag
    Nov 15 '18 at 11:11













  • James, yes this should be a list of nearby userids, how did you know? :D. Do you have a place where I can see how to do that efficiently?

    – TTnote
    Nov 15 '18 at 11:14











  • @JamesPoag, as a beginner, I can't find the relation between the onWrite in the link to the onWrite in Java code for android. How would I use onWrite inside my project in Android Studio?

    – TTnote
    Nov 15 '18 at 11:28








1




1





How about using a transaction to execute both CRUD operations sort of "together"? informit.com/articles/article.aspx?p=2731932&seqNum=2

– giro
Nov 15 '18 at 11:05







How about using a transaction to execute both CRUD operations sort of "together"? informit.com/articles/article.aspx?p=2731932&seqNum=2

– giro
Nov 15 '18 at 11:05















But this is firebase database, or I am completely off? (Sorry I am a beginner)

– TTnote
Nov 15 '18 at 11:09





But this is firebase database, or I am completely off? (Sorry I am a beginner)

– TTnote
Nov 15 '18 at 11:09




1




1





Use a separate branch for users and places. users->user_1->{location, coords}. Perhaps the location is list of nearby userids locations->location->{user_id : timestamp}. Throw an onWrite trigger to remove the userid from the old location.

– James Poag
Nov 15 '18 at 11:11







Use a separate branch for users and places. users->user_1->{location, coords}. Perhaps the location is list of nearby userids locations->location->{user_id : timestamp}. Throw an onWrite trigger to remove the userid from the old location.

– James Poag
Nov 15 '18 at 11:11















James, yes this should be a list of nearby userids, how did you know? :D. Do you have a place where I can see how to do that efficiently?

– TTnote
Nov 15 '18 at 11:14





James, yes this should be a list of nearby userids, how did you know? :D. Do you have a place where I can see how to do that efficiently?

– TTnote
Nov 15 '18 at 11:14













@JamesPoag, as a beginner, I can't find the relation between the onWrite in the link to the onWrite in Java code for android. How would I use onWrite inside my project in Android Studio?

– TTnote
Nov 15 '18 at 11:28





@JamesPoag, as a beginner, I can't find the relation between the onWrite in the link to the onWrite in Java code for android. How would I use onWrite inside my project in Android Studio?

– TTnote
Nov 15 '18 at 11:28












1 Answer
1






active

oldest

votes


















2














When using your current database structure, in order to change the location of a user, you perform 3 operations:





  1. Read the current location of your user:



    database -> places -> location A -> user1



  2. Copy current location to the new location:



    database -> places -> location B -> user1



  3. Remove the old location:



    database -> places -> location A



This can be solved by using a single update operation. In this case you should consider change your database structure a little bit like this:



Firebase-root
|
--- users
|
--- uid
|
--- lat: 48.856151
|
--- long: 2.298115


In order to update the location of the user, you should only update this two properties like in the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
Map<String, Object> map = new HashMap<>();
map.put("lat", 41.890362);
map.put("long", 12.492263);
uidRef.updateChildren(map);





share|improve this answer
























  • when you use the updateChildren(map) does it automatically remove the old record from its old place in the database?

    – TTnote
    Nov 15 '18 at 12:02








  • 1





    In this case, there is no old place and new place. There is a single place where you create a single update operation. With other words, you update current locations cooredinates with the new onces. That's it!

    – Alex Mamo
    Nov 15 '18 at 12:03













  • Oh true, just noticed that. But in this case, What is the strategy to find near by userIds by the lat and long? Could you please add a code for that? How to traverse in the tree for only nearby users when the database is structured like in your code? I don't know where to start! Because if there are many users it's gonna be inifite search. At first you check all users that are nearby according to lat, then if lat is correct you need to match long as well..

    – TTnote
    Nov 15 '18 at 12:06








  • 1





    This is really a bit too broad to reasonably be able to answer in single comment here. Your initial question was How to update user in the following structure So hope I was able to provide you the best solution. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.

    – Alex Mamo
    Nov 15 '18 at 12:12








  • 1





    If you think that my answer helped you for what you have asked first, please consider accepting it. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 12:12













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317966%2fhow-to-update-user-in-the-following-structure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














When using your current database structure, in order to change the location of a user, you perform 3 operations:





  1. Read the current location of your user:



    database -> places -> location A -> user1



  2. Copy current location to the new location:



    database -> places -> location B -> user1



  3. Remove the old location:



    database -> places -> location A



This can be solved by using a single update operation. In this case you should consider change your database structure a little bit like this:



Firebase-root
|
--- users
|
--- uid
|
--- lat: 48.856151
|
--- long: 2.298115


In order to update the location of the user, you should only update this two properties like in the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
Map<String, Object> map = new HashMap<>();
map.put("lat", 41.890362);
map.put("long", 12.492263);
uidRef.updateChildren(map);





share|improve this answer
























  • when you use the updateChildren(map) does it automatically remove the old record from its old place in the database?

    – TTnote
    Nov 15 '18 at 12:02








  • 1





    In this case, there is no old place and new place. There is a single place where you create a single update operation. With other words, you update current locations cooredinates with the new onces. That's it!

    – Alex Mamo
    Nov 15 '18 at 12:03













  • Oh true, just noticed that. But in this case, What is the strategy to find near by userIds by the lat and long? Could you please add a code for that? How to traverse in the tree for only nearby users when the database is structured like in your code? I don't know where to start! Because if there are many users it's gonna be inifite search. At first you check all users that are nearby according to lat, then if lat is correct you need to match long as well..

    – TTnote
    Nov 15 '18 at 12:06








  • 1





    This is really a bit too broad to reasonably be able to answer in single comment here. Your initial question was How to update user in the following structure So hope I was able to provide you the best solution. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.

    – Alex Mamo
    Nov 15 '18 at 12:12








  • 1





    If you think that my answer helped you for what you have asked first, please consider accepting it. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 12:12


















2














When using your current database structure, in order to change the location of a user, you perform 3 operations:





  1. Read the current location of your user:



    database -> places -> location A -> user1



  2. Copy current location to the new location:



    database -> places -> location B -> user1



  3. Remove the old location:



    database -> places -> location A



This can be solved by using a single update operation. In this case you should consider change your database structure a little bit like this:



Firebase-root
|
--- users
|
--- uid
|
--- lat: 48.856151
|
--- long: 2.298115


In order to update the location of the user, you should only update this two properties like in the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
Map<String, Object> map = new HashMap<>();
map.put("lat", 41.890362);
map.put("long", 12.492263);
uidRef.updateChildren(map);





share|improve this answer
























  • when you use the updateChildren(map) does it automatically remove the old record from its old place in the database?

    – TTnote
    Nov 15 '18 at 12:02








  • 1





    In this case, there is no old place and new place. There is a single place where you create a single update operation. With other words, you update current locations cooredinates with the new onces. That's it!

    – Alex Mamo
    Nov 15 '18 at 12:03













  • Oh true, just noticed that. But in this case, What is the strategy to find near by userIds by the lat and long? Could you please add a code for that? How to traverse in the tree for only nearby users when the database is structured like in your code? I don't know where to start! Because if there are many users it's gonna be inifite search. At first you check all users that are nearby according to lat, then if lat is correct you need to match long as well..

    – TTnote
    Nov 15 '18 at 12:06








  • 1





    This is really a bit too broad to reasonably be able to answer in single comment here. Your initial question was How to update user in the following structure So hope I was able to provide you the best solution. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.

    – Alex Mamo
    Nov 15 '18 at 12:12








  • 1





    If you think that my answer helped you for what you have asked first, please consider accepting it. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 12:12
















2












2








2







When using your current database structure, in order to change the location of a user, you perform 3 operations:





  1. Read the current location of your user:



    database -> places -> location A -> user1



  2. Copy current location to the new location:



    database -> places -> location B -> user1



  3. Remove the old location:



    database -> places -> location A



This can be solved by using a single update operation. In this case you should consider change your database structure a little bit like this:



Firebase-root
|
--- users
|
--- uid
|
--- lat: 48.856151
|
--- long: 2.298115


In order to update the location of the user, you should only update this two properties like in the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
Map<String, Object> map = new HashMap<>();
map.put("lat", 41.890362);
map.put("long", 12.492263);
uidRef.updateChildren(map);





share|improve this answer













When using your current database structure, in order to change the location of a user, you perform 3 operations:





  1. Read the current location of your user:



    database -> places -> location A -> user1



  2. Copy current location to the new location:



    database -> places -> location B -> user1



  3. Remove the old location:



    database -> places -> location A



This can be solved by using a single update operation. In this case you should consider change your database structure a little bit like this:



Firebase-root
|
--- users
|
--- uid
|
--- lat: 48.856151
|
--- long: 2.298115


In order to update the location of the user, you should only update this two properties like in the following lines of code:



String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child(uid);
Map<String, Object> map = new HashMap<>();
map.put("lat", 41.890362);
map.put("long", 12.492263);
uidRef.updateChildren(map);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:48









Alex MamoAlex Mamo

44.8k82863




44.8k82863













  • when you use the updateChildren(map) does it automatically remove the old record from its old place in the database?

    – TTnote
    Nov 15 '18 at 12:02








  • 1





    In this case, there is no old place and new place. There is a single place where you create a single update operation. With other words, you update current locations cooredinates with the new onces. That's it!

    – Alex Mamo
    Nov 15 '18 at 12:03













  • Oh true, just noticed that. But in this case, What is the strategy to find near by userIds by the lat and long? Could you please add a code for that? How to traverse in the tree for only nearby users when the database is structured like in your code? I don't know where to start! Because if there are many users it's gonna be inifite search. At first you check all users that are nearby according to lat, then if lat is correct you need to match long as well..

    – TTnote
    Nov 15 '18 at 12:06








  • 1





    This is really a bit too broad to reasonably be able to answer in single comment here. Your initial question was How to update user in the following structure So hope I was able to provide you the best solution. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.

    – Alex Mamo
    Nov 15 '18 at 12:12








  • 1





    If you think that my answer helped you for what you have asked first, please consider accepting it. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 12:12





















  • when you use the updateChildren(map) does it automatically remove the old record from its old place in the database?

    – TTnote
    Nov 15 '18 at 12:02








  • 1





    In this case, there is no old place and new place. There is a single place where you create a single update operation. With other words, you update current locations cooredinates with the new onces. That's it!

    – Alex Mamo
    Nov 15 '18 at 12:03













  • Oh true, just noticed that. But in this case, What is the strategy to find near by userIds by the lat and long? Could you please add a code for that? How to traverse in the tree for only nearby users when the database is structured like in your code? I don't know where to start! Because if there are many users it's gonna be inifite search. At first you check all users that are nearby according to lat, then if lat is correct you need to match long as well..

    – TTnote
    Nov 15 '18 at 12:06








  • 1





    This is really a bit too broad to reasonably be able to answer in single comment here. Your initial question was How to update user in the following structure So hope I was able to provide you the best solution. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.

    – Alex Mamo
    Nov 15 '18 at 12:12








  • 1





    If you think that my answer helped you for what you have asked first, please consider accepting it. I'd appreciate it. Thanks!

    – Alex Mamo
    Nov 15 '18 at 12:12



















when you use the updateChildren(map) does it automatically remove the old record from its old place in the database?

– TTnote
Nov 15 '18 at 12:02







when you use the updateChildren(map) does it automatically remove the old record from its old place in the database?

– TTnote
Nov 15 '18 at 12:02






1




1





In this case, there is no old place and new place. There is a single place where you create a single update operation. With other words, you update current locations cooredinates with the new onces. That's it!

– Alex Mamo
Nov 15 '18 at 12:03







In this case, there is no old place and new place. There is a single place where you create a single update operation. With other words, you update current locations cooredinates with the new onces. That's it!

– Alex Mamo
Nov 15 '18 at 12:03















Oh true, just noticed that. But in this case, What is the strategy to find near by userIds by the lat and long? Could you please add a code for that? How to traverse in the tree for only nearby users when the database is structured like in your code? I don't know where to start! Because if there are many users it's gonna be inifite search. At first you check all users that are nearby according to lat, then if lat is correct you need to match long as well..

– TTnote
Nov 15 '18 at 12:06







Oh true, just noticed that. But in this case, What is the strategy to find near by userIds by the lat and long? Could you please add a code for that? How to traverse in the tree for only nearby users when the database is structured like in your code? I don't know where to start! Because if there are many users it's gonna be inifite search. At first you check all users that are nearby according to lat, then if lat is correct you need to match long as well..

– TTnote
Nov 15 '18 at 12:06






1




1





This is really a bit too broad to reasonably be able to answer in single comment here. Your initial question was How to update user in the following structure So hope I was able to provide you the best solution. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.

– Alex Mamo
Nov 15 '18 at 12:12







This is really a bit too broad to reasonably be able to answer in single comment here. Your initial question was How to update user in the following structure So hope I was able to provide you the best solution. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.

– Alex Mamo
Nov 15 '18 at 12:12






1




1





If you think that my answer helped you for what you have asked first, please consider accepting it. I'd appreciate it. Thanks!

– Alex Mamo
Nov 15 '18 at 12:12







If you think that my answer helped you for what you have asked first, please consider accepting it. I'd appreciate it. Thanks!

– Alex Mamo
Nov 15 '18 at 12:12






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317966%2fhow-to-update-user-in-the-following-structure%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine