GFCircleQuery doesn't return any result - swift

I useGeofire for iOS. I faced the following problem, I use Geofire to get the cards and users that are next to the current user. For cards, the result is correct, but for users I do not get any results, although the data structure in Firebase Database is the same for storing the location of cards and users. Please tell me how can I solve this problem?
My structure data
"cardLocation" : {
"-KjLxK0q39JnT2MZEalB" : {
".priority" : "v17wefy8z",
"g" : "v17wefy8z",
"l" : [ *****, ****** ]
},
"-KjM7_5sPkoruwoTvRzR" : {
".priority" : "9q5c2ypf3",
"g" : "9q5c2ypf3",
"l" : [ ******, -****** ]
},
"-KjNDq8nQ2Ffjr9M_1a9" : {
".priority" : "9q59x2vc6",
"g" : "9q59x2vc6",
"l" : [ *******, -***** ]
}
},
"userLocations" : {
"1Cix149ThIOG1ULPVjyy0LyTxbe2" : {
".priority" : "87zc0d2j5",
"g" : "87zc0d2j5",
"l" : [ ******, -**** ]
},

I did not correctly use geofire by adding additional property to its model. Now I add only CLLocation to it and it works for me.
let geofireRef = Database.database().reference().child(MainGateways.cardLocation.rawValue)
guard let geoFire = GeoFire(firebaseRef: geofireRef) else { return }
dispatchGroup.enter()
geoFire.setLocation(cardLocation, forKey: cardModelRef.key) { (error) in
commonError = error
dispatchGroup.leave()
}

Related

How to get the total number of times each object has been referenced in a collection?

So what i am trying to find is how many time each kite was flown by each person, the db looks like this:
{
"_id" : ObjectId("5bccf4d7ac8fa95f43a964c2"),
"KITE" : {
"registration" : "DTH498"
}
}
{
"_id" : ObjectId("5bccf4d7ac8fa95f43a964c3"),
"KITE" : {
"registration" : "HKJ607"
}
}
{
"_id" : ObjectId("5bccf4d7ac8fa95f43a964c4"),
"KITE" : {
"registration" : "GCF21"
}
}
{
"_id" : ObjectId("5bccf4d7ac8fa95f43a964cf"),
"PERSON" : {
"name" : "H.Y",
"used" : [
{
"registration" : "DTH498"
},
{
"registration" : "HKJ607"
},
{
"registration" : "GCF21"
}
]
}
}
{
"_id" : ObjectId("5bccf4d7ac8fa95f43a9leo5"),
"PERSON" : {
"name" : "T.G",
"used" : [
{
"registration" : "DTH498"
},
{
"registration" : "HKJ607"
},
{
"registration" : "GCF21"
}
]
}
}
{
"_id" : ObjectId("5bccf4d7ac8fro4943a01pak"),
"PERSON" : {
"name" : "X.L",
"used" : [
{
"registration" : "DTH498"
},
{
"registration" : "HKJ607"
},
{
"registration" : "GCF21"
}
]
}
}
Right now i can only use the aggregation framework, with that said i've managed to list all kites that have been used:
db.data.aggregate([
{"$unwind":"$PERSON.used"},
{"$group":{"_id":"$PERSON.used.registration"}}
]).pretty();
The result i'm trying to get to would have the registration of the kite and the number of times it has been used overall.
I'm not sure how it would look but, what im thinking is:
1. Somehow get all used array objects registrations into a new array so it one big list of all the times each kite has been used
2. Group the array by registration and use sum to display how many duplicates each group had.
I DID IT, WOOOO!
so if anyone ever stumbles upon my question, heres the answer.
First unwind the used array
Second project only the registrations
Third group with an _id of registration and count with $sum
db.data.aggregate([
{"$unwind":"$PERSON.used"},
{"$project":{"registration":"$PERSON.used.registration","_id":0}},
{"$group":{"_id":"$registration", "count":{"$sum":1}}}
]);

Firebase order by child returning results that do not have the child

I have a firebase data structure like the following for direct message chats:
{
"dms" : {
"meta" : {
"1-2" : {
"lastMessage" : "Inadvertently",
"lastMessageFrom" : 5,
"lastMessageKey" : "-KhUHUqK9WfUJcjs2fQ-",
"timestamp" : 1491952336485,
"users" : {
"uid-1" : {
"lastMessageRead" : {
"key" : "-KhStVBVIfRGepiWt6cn"
}
},
"uid-2" : {
"lastMessageRead" : {
"key" : "-KhSrksAgg_2TqmcLjsz"
}
}
}
}
}
}
}
If the user id is 1, I only want to retrieve nodes where users.uid-1 exists. In Swift, I thought this looked like:
FIRDatabase.database().reference().child("dms").child("meta").queryOrdered(byChild: "users/uid-3").observe(.value, with: { (snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
...
but that is returning every dms node. For example, I am querying for uid-3 above but it will return the node in the data structure example I provided. What am I doing wrong?
I ended up changing my structure to this:
{
"meta" : {
"1-2" : {
"lastMessage" : "Dude....",
"lastMessageFrom" : 2,
"lastMessageKey" : "-KhXpXDn7et6mgFJRkWb",
"timestamp" : 1492011852859,
"users" : {
"uid-1" : true,
"uid-2" : true
},
"users-meta" : {
"uid-2" : {
"lastMessageRead" : {
"key" : "-KhXpXDn7et6mgFJRkWb"
}
}
}
}
}
}
And my query to require that the child value equal true. I guess in their docs when they explain how null values will be ordered first, they mean null or missing?

Retrieving Keys From GeoFire within Radius in Swift

I have the following Firebase Database JSON data structure:
{
"Post List" : {
"-KUlvg8mCEGfY5ZSKCou" : {
"addedByUser" : "7llQbPdy2NV7aO337h7ap0qLOhC3",
"content" : "Post1",
"cost" : "$450",
"duration" : "Daily",
"latitude" : "25.0879721592719",
"longitude" : "55.1487715855458",
"number" : "01234567890",
"timestamp" : "Posted on: Sun 23 Oct"
}
},
"Post Locations" : {
"-KUlvg8mCEGfY5ZSKCou" : {
".priority" : "thrnwznh58",
"g" : "thrnwznh58",
"l" : [ 25.0879721592719, 55.1487715855458 ]
}
},
"User Locations" : {
"7llQbPdy2NV7aO337h7ap0qLOhC3" : {
".priority" : "thrnwypqu9",
"g" : "thrnwypqu9",
"l" : [ 25.0829547120321, 55.1505315855337 ]
}
}
}
I am looking to have a GeoFire query which will return all the keys [FIRAutoID's] within a radius of 1KM, for e.g. of "User Locations" these Posts are static and once the user location is set it will also be fixed. From the radius query result I am expecting to return "-KUlvg8mCEGfY5ZSKCou" so I can then make a reference the Post Details. Hope this makes sense. Many thanks, D. Cant find much online about GeoFire... looks like its still in the initial stages?...
Figured it out
func geoFireQuery() {
let circleQuery = geoFire.query(at: self.myLocation, withRadius: 0.5)
_ = circleQuery!.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in
print (key!)
})
circleQuery?.observeReady({
print("All initial data has been loaded and events have been fired!")
})
}
this seems to be giving me what I need. Now to reference those keys to the other part of the FBDB. :)

MongoDb : Update a Record & push to an array. Cannot update 'addresses.0' and 'addresses.0.insertupdate' at the same time

I have a collection names with this schema.
{
name: "A1",
addresses :
[
{
add1 : "a1",
add2 : "a2",
insertupdate:[
{
"mode" : "i" ,
"user" : "abcd"
}
]
}
,
{
add1 : "a3",
add2 : "a4",
insertupdate:[
{
"mode" : "i",
"user" : "pqrs"
}
]
}
],
insertupdate:[
{
"mode" : "i",
"user" : "pqrs"
}
]
}
What I am trying to do :
I want to update record add1 = "a1" as add2 = "a5" & push a record inside addresses.insertupdate as well as insertupdate.
What i tried :
db.names.update({name:"A1",addresses.add1:"a1"},
{
$set : {
addresses.$ :
{
"add2":"a5"
}
},
$push : {
"insertupdate" : {"mode":"u","user":"xyz"},
"addresses.$.insertupdate" : {"mode":"u","user":"xyz"}
}
}
);
Error I got :
Cannot update 'addresses.0' and 'addresses.0.insertupdate' at the same
time
Edit :
This command was successful. However it wont let me set my values. i.e. I need a way out to use a set & push with the positional operator at the same time.
db.names.update(
{"name":"A1", "addresses.add1":"a1"},
{$push:{
"insertupdate":{"mode":"u","user":"xyz"},
"addresses.$.insertupdate":{"mode":"u","user":"xyz"}
}
})
A comment in one of my questions pointed me to the answer mongodb update using positional operator $ removes other fields :
db.names.update({name:"A1",addresses.add1:"a1"},
{
$set : {
"addresses.$.add2" : "a5"
},
$push : {
"insertupdate" : {"mode":"u","user":"xyz"},
"addresses.$.insertupdate" : {"mode":"u","user":"xyz"}
}
}
);

Using 'findAndModify' on mongodb within multiple nested array (complex)

I have the next document in a collection:
{
"_id" : ObjectId("546a7a0f44aee82db8469f6d"),
...
"valoresVariablesIterativas" : [
{
"asignaturaVO" : {
"_id" : ObjectId("546a389c44aee54fc83112e9")
},
"valoresEstaticos" : {
"IT_VAR3" : "",
"IT_VAR1" : "",
"IT_VAR2" : "asdasd"
},
"valoresPreestablecidos" : {
"IT_ASIGNATURA" : "Matemáticas",
"IT_NOTA_DEFINITIVA_ASIGNATURA" : ""
}
},
{
"asignaturaVO" : {
"_id" : ObjectId("546a3d8d44aee54fc83112fa")
},
"valoresEstaticos" : {
"IT_VAR3" : "",
"IT_VAR1" : "",
"IT_VAR2" : ""
},
"valoresPreestablecidos" : {
"IT_ASIGNATURA" : "Español",
"IT_NOTA_DEFINITIVA_ASIGNATURA" : ""
}
}
]
...
}
I want modify an element of the valoresEstaticos, I know the fields "_id", "asignaturaVO", and the key of the item valoresEstaticos that I want modify.
Which is the correct query for this?, I have this:
db.myCollection.findAndModify({
query:{"_id" : ObjectId("546a7a0f44aee82db8469f6d")},
update: {
{valoresVariablesIterativas.asignaturaVO._id: ObjectId("546a389c44aee54fc83112e9")},
{ $set: {}}
}
})
but I dont know how to build a query :(
Help me please, Thank you very much!
You can just use update.
db.myCollection.update(
{ "_id" : ObjectId("546a7a0f44aee82db8469f6d"), "valoresVariablesIterativas.asignaturaVO._id" : ObjectId("546a389c44aee54fc83112e9") },
{ "$set" : { "valoresVariablesIterativas.$.valoresEstaticos.IT_VAR3" : 99 } }
)
assuming you want to update key IT_VAR3. The key is the positional update operator $. The condition on the array in the query portion of the update is redundant for finding the document, but necessary to use the $ to update the correct array element.