Are firebase generated keys ordered? - firebase

I have a an array of ids which are generated using the firebase.ref().push() method.
For example -
myarray =
[-KoDeiUiqsMKDuXuwhzi,
-KoDrz6ngJtNrr51tguD,
-KoDyC-_ZeSIAN1_oFk4]
I have a node of items some of which match these ids -
/mynode
--KoDeiUiqsMKDuXuwhzi
-itemprop1 : "abc"
-itemprop2 : "xyz"
-id : "KoDeiUiqsMKDuXuwhzi"
--KnurYH554YNW6bcWch5
-itemprop1 : "opq"
-itemprop2 : "zyc"
-id : "KnurYH554YNW6bcWch5"
--KoDrz6ngJtNrr51tguD
-itemprop1 : "mon"
-itemprop2 : "ooo"
-id : "KoDrz6ngJtNrr51tguD"
..
..
..
..
..and so on.. basically the array of id's is a subset of this node.
I want to retreive a all objects from this node where the id matches in the array.
Currently the way i am doing is like this -
firebase.database().ref("mynode").once('value').then(function(snap){
snap.forEach(function(childsnap){
if(myarray.indexOf(childsnap.key) != -1){
//add to result
}
})
})
Although this code works but i think i can use orderByChild, startAt and endAt filtering to get the list of objects.
Is there a way to get the result by filtering instead of getting all data and then filtering out? Will it be more efficient than this code?

Related

Extract values from web service JSON response with JSONPath

I have a JSON response from web service that looks something like this :
[
{
"id":4,
"sourceID":null,
"subject":"SomeSubjectOne",
"category":"SomeCategoryTwo",
"impact":null,
"status":"completed"
},
{
"id":12,
"sourceID":null,
"subject":"SomeSubjectTwo",
"category":"SomeCategoryTwo",
"impact":null,
"status":"assigned"
}
]
What I need to do is extract the subjects from all of the entities by using JSONPATH query.
How can I get these results :
Subject from the first item - SomeSubjectOne
Filter on specific subject value from all entities (SomeSubjectTwo for example)
Get Subjects from all entities
Goessner's orinial JSONPath article is a good reference point and all implementations more or less stick to the suggested query syntax. However, implementations like Jayway JsonPath/Java, JSONPath-Plus/JavaScript, flow-jsonpath/PHP may behave a little differently in some areas. That's why it can be important to know what implementation you are actually using.
Subject from the first item
Just use an index to select the desired array element.
$.[0].subject
Returns:
SomeSubjectOne
Specific subject value
First, go for any elements .., check those with a subject [?(#.subject] and use == '..' for comparison.
$..[?(#.subject == 'SomeSubjectTwo')]
Returns
[ {
"id" : 12,
"sourceID" : null,
"subject" : "SomeSubjectTwo",
"category" : "SomeCategoryTwo",
"impact" : null,
"status" : "assigned" } ]*
Get all subjects
$.[*].subject
or simply
$..subject
Returns
[ "SomeSubjectOne", "SomeSubjectTwo" ]

Using mongo db in R

I am trying to write R script in which I have to do some operations on mongo database. Therefore, I have a few questions:
How to use double condition? I know how to use single condtion
mongoDB$find(query = '{"id" : { "$in" : ["1","2"]}}')
mongoDB$find(query = '{"date" : { "$in" : "2019-08-09"}}')
How should I connect both conditions in one query? How to write such code?
How to use parameter in mongo code? In my script I will have vector with dynamic number of IDs. How I should write query? I am looking for something like:
VectorWithIDs <- c(1:1000)
mongoDB$find(query = '{"id" : { "$in" : VectorWithIDs}}')
Any Ideas how to solve both problems? Thanks in advance!
Just do the query with a "," as a separation token.
For example:
mongoDB$find(query = '{"id" : { "$in" : ["1","2"]}, '': "date" : { "$in" : "2019-08-09"}}'}')

mongolite: how to perform a LIKE query?

I want to perform a partial match query on a MongoDB in R. I've tried to specify a query that matches the MongoDB query format like so:
library(mongolite)
foo <- mongo(url = "myConnectionString")
bar <- foo$find(
query = '{"_id": /idContainsThis/}',
fields = '{}'
)
But when I try this, I get the following error:
Error: Invalid JSON object: {"_id": /idContainsThis/}
I can't use this solution because if I put quotes round the term, the / is taken as a string literal, not the wildcard I need.
Does anyone know how to make this work with mongolite?
You'll have to use the regex function like this
query = '{"_id": { "$regex" : "idContainsThis", "$options" : "i" }}'
The "$options" : "i" is in case you want it to be case insensitive.
However I am not sure if this will work on an _id

Meteor query based on the value of elements in an array inside an object

I am new to meteor and mongoDB and have been searching for an answer to this question for some time without any luck.
I have multiple documents in MongoDB similar to the one below:
{
"_id" : ObjectId("5abac4ea0c31d26804421371"),
"Points" : [
{
"Value" : 6.869752766626993,
"Time" : 1522284528946
},
{
"Value" : 3.9014587731230477,
"Time" : 1522284543946
},
{
"Value" : 1.2336926618519772,
"Time" : 1522284558946
},
{
"Value" : 6.504837583667155,
"Time" : 1522284573946
},
{
"Value" : 9.824138227740864,
"Time" : 1522284588946
},
{
"Value" : 9.707480757899235,
"Time" : 1522284603946
},
{
"Value" : 4.6122167850338105,
"Time" : 1522284618946
}
]
}
How can I implement a query in meteor that returns an array containing all the Points from all documents with 'Time' field greater than certain value?
As Jankapunkt has pointed out in his comment, it might be a lot easier and better if you created a new collection Points where each document includes only Value and Time attributes. The given example would then become seven separate documents rather than a single array.
It does nevertheless happen, that we want to query documents according to some inner values, e.g. attributes in objects in arrays.
Taken from the mongodb documentation on querying embedded documents, we can just use dot notation for this.
If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document.
Such as for your question (assuming Points to be the name of your collection):
db.points.find( { 'Points.Time': { $gte: 123412341234 } } )
Which looks almost identical in Meteor:
Points.find({ 'Points.Time': { $gte: 123412341234 } })

Does Firebase "GeoFire" Enforce Identical Keys Between Nodes with Geo and non-Geo Data?

The database structure for a Firebase "GeoFire" node must look like this (source)
"items" : {
<itemId> : {
"someData" : "someData",
...
}
},
"items_location" : {
<itemId> : {
<geofireData> ...
}
}
But one limitation of Geofire is that only single points can be stored and queried, and no objects like polygons. That's easy to work around - my code can query nearby points, and then reassemble the simple rectangles based on having the same key.
But in splitting my rectangles into individual points, I've created a GeoFire key with the following format
ABCD_0
Where ABCD is the original Primary Key of the rectangle, and _0 indicates which corner, so as to have each point with a unique key. One rectangle is represented in GeoFire as
"items" : {
<ABCD_0> : {<objectData>},
<ABCD_1> : {<objectData>},
<ABCD_2> : {<objectData>},
<ABCD_3> : {<objectData>}
},
"items_location" : {
<ABCD_0> : {<geofireData 0>},
<ABCD_1> : {<geofireData 1>},
<ABCD_2> : {<geofireData 2>},
<ABCD_3> : {<geofireData 3>}
}
But then to force identical keys in items and items_location, <objectData> is 4x redundant.
In order to decrease data volume, I'd like to use the original Primary Key in the items node, and then replicate the key with the _X structure for the items_location node. The App would then query GeoFire, get a list of (4) nearby keys, and then string-parse ABCD_X into ABCD, which it would use for the subsequent query.
"items" : {
<ABCD> : {<objectData>},
},
"items_location" : {
<ABCD_0> : {<geofireData 0>},
<ABCD_1> : {<geofireData 1>},
<ABCD_2> : {<geofireData 2>},
<ABCD_3> : {<geofireData 3>}
}
Will this break how GeoFire stores, indexes, retrieves and offlines data?
I'm especially concerned about how small sets of data is synchronized offline for individual apps. The entire geo-dataset is too large for a single app to store in its entirety offline.

Resources