Filter firebase records by the array value of a specific property












0














I am playing around with firebase functions and I have a question. The structure of my firebase DB is as follows:



 myDb
|
Users
|
000001
|
Name: Sam
|
Items: [1,2,3]
|
000002
|
Name: Jim
|
Items: [3,4,5]


And what I want to do is to filter all of the users which have 3 inside of their Items value. I didn't find anything similar to: this SO post on their documentations for filtering data. Maybe because it is not a simple value. I don't know. I am now wondering how to perform this particular filtering.










share|improve this question






















  • How do you store the array? Is the index of the element the key property: items: { 0: 1, 1: 2, 2:3 }
    – Hristo Eftimov
    Nov 13 '18 at 8:47
















0














I am playing around with firebase functions and I have a question. The structure of my firebase DB is as follows:



 myDb
|
Users
|
000001
|
Name: Sam
|
Items: [1,2,3]
|
000002
|
Name: Jim
|
Items: [3,4,5]


And what I want to do is to filter all of the users which have 3 inside of their Items value. I didn't find anything similar to: this SO post on their documentations for filtering data. Maybe because it is not a simple value. I don't know. I am now wondering how to perform this particular filtering.










share|improve this question






















  • How do you store the array? Is the index of the element the key property: items: { 0: 1, 1: 2, 2:3 }
    – Hristo Eftimov
    Nov 13 '18 at 8:47














0












0








0







I am playing around with firebase functions and I have a question. The structure of my firebase DB is as follows:



 myDb
|
Users
|
000001
|
Name: Sam
|
Items: [1,2,3]
|
000002
|
Name: Jim
|
Items: [3,4,5]


And what I want to do is to filter all of the users which have 3 inside of their Items value. I didn't find anything similar to: this SO post on their documentations for filtering data. Maybe because it is not a simple value. I don't know. I am now wondering how to perform this particular filtering.










share|improve this question













I am playing around with firebase functions and I have a question. The structure of my firebase DB is as follows:



 myDb
|
Users
|
000001
|
Name: Sam
|
Items: [1,2,3]
|
000002
|
Name: Jim
|
Items: [3,4,5]


And what I want to do is to filter all of the users which have 3 inside of their Items value. I didn't find anything similar to: this SO post on their documentations for filtering data. Maybe because it is not a simple value. I don't know. I am now wondering how to perform this particular filtering.







firebase react-native google-cloud-functions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 7:37









user2128702user2128702

5161831




5161831












  • How do you store the array? Is the index of the element the key property: items: { 0: 1, 1: 2, 2:3 }
    – Hristo Eftimov
    Nov 13 '18 at 8:47


















  • How do you store the array? Is the index of the element the key property: items: { 0: 1, 1: 2, 2:3 }
    – Hristo Eftimov
    Nov 13 '18 at 8:47
















How do you store the array? Is the index of the element the key property: items: { 0: 1, 1: 2, 2:3 }
– Hristo Eftimov
Nov 13 '18 at 8:47




How do you store the array? Is the index of the element the key property: items: { 0: 1, 1: 2, 2:3 }
– Hristo Eftimov
Nov 13 '18 at 8:47












1 Answer
1






active

oldest

votes


















0














Array contain filters aren't possible in Firebase Realtime Database but it is a recent addition to Firestore.



firebase.firestore().collection('users').where('items', 'array-contains', 3)


In the Firebase Realtime Database you would want to structure them as an object which you could query on



{
name: "Jim",
items: {
1: true,
2: true,
3: true
}
}


and then the query would be like this



firebase.database().ref('users').orderByChild('items/3').equalTo(true);





share|improve this answer





















  • Would it be possible to have the keys as strings (e.g. {"str1":1, "str2":2}) and then to have .orderByChild('items/str1').equalTo(1); ?
    – user2128702
    Nov 13 '18 at 19:30










  • No reason why not, it will work
    – Jack Woodward
    Nov 13 '18 at 20:05











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%2f53276031%2ffilter-firebase-records-by-the-array-value-of-a-specific-property%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









0














Array contain filters aren't possible in Firebase Realtime Database but it is a recent addition to Firestore.



firebase.firestore().collection('users').where('items', 'array-contains', 3)


In the Firebase Realtime Database you would want to structure them as an object which you could query on



{
name: "Jim",
items: {
1: true,
2: true,
3: true
}
}


and then the query would be like this



firebase.database().ref('users').orderByChild('items/3').equalTo(true);





share|improve this answer





















  • Would it be possible to have the keys as strings (e.g. {"str1":1, "str2":2}) and then to have .orderByChild('items/str1').equalTo(1); ?
    – user2128702
    Nov 13 '18 at 19:30










  • No reason why not, it will work
    – Jack Woodward
    Nov 13 '18 at 20:05
















0














Array contain filters aren't possible in Firebase Realtime Database but it is a recent addition to Firestore.



firebase.firestore().collection('users').where('items', 'array-contains', 3)


In the Firebase Realtime Database you would want to structure them as an object which you could query on



{
name: "Jim",
items: {
1: true,
2: true,
3: true
}
}


and then the query would be like this



firebase.database().ref('users').orderByChild('items/3').equalTo(true);





share|improve this answer





















  • Would it be possible to have the keys as strings (e.g. {"str1":1, "str2":2}) and then to have .orderByChild('items/str1').equalTo(1); ?
    – user2128702
    Nov 13 '18 at 19:30










  • No reason why not, it will work
    – Jack Woodward
    Nov 13 '18 at 20:05














0












0








0






Array contain filters aren't possible in Firebase Realtime Database but it is a recent addition to Firestore.



firebase.firestore().collection('users').where('items', 'array-contains', 3)


In the Firebase Realtime Database you would want to structure them as an object which you could query on



{
name: "Jim",
items: {
1: true,
2: true,
3: true
}
}


and then the query would be like this



firebase.database().ref('users').orderByChild('items/3').equalTo(true);





share|improve this answer












Array contain filters aren't possible in Firebase Realtime Database but it is a recent addition to Firestore.



firebase.firestore().collection('users').where('items', 'array-contains', 3)


In the Firebase Realtime Database you would want to structure them as an object which you could query on



{
name: "Jim",
items: {
1: true,
2: true,
3: true
}
}


and then the query would be like this



firebase.database().ref('users').orderByChild('items/3').equalTo(true);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 9:56









Jack WoodwardJack Woodward

61139




61139












  • Would it be possible to have the keys as strings (e.g. {"str1":1, "str2":2}) and then to have .orderByChild('items/str1').equalTo(1); ?
    – user2128702
    Nov 13 '18 at 19:30










  • No reason why not, it will work
    – Jack Woodward
    Nov 13 '18 at 20:05


















  • Would it be possible to have the keys as strings (e.g. {"str1":1, "str2":2}) and then to have .orderByChild('items/str1').equalTo(1); ?
    – user2128702
    Nov 13 '18 at 19:30










  • No reason why not, it will work
    – Jack Woodward
    Nov 13 '18 at 20:05
















Would it be possible to have the keys as strings (e.g. {"str1":1, "str2":2}) and then to have .orderByChild('items/str1').equalTo(1); ?
– user2128702
Nov 13 '18 at 19:30




Would it be possible to have the keys as strings (e.g. {"str1":1, "str2":2}) and then to have .orderByChild('items/str1').equalTo(1); ?
– user2128702
Nov 13 '18 at 19:30












No reason why not, it will work
– Jack Woodward
Nov 13 '18 at 20:05




No reason why not, it will work
– Jack Woodward
Nov 13 '18 at 20:05


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53276031%2ffilter-firebase-records-by-the-array-value-of-a-specific-property%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