Id like to get a snapshot with snapshot.key as the UID if the snapshot.value is "pending". Store that into an array. Then i'd like to loop through the array of UID and pull of all the details from .child("users") i.e. "email", "name" & "profileURL". Not sure what the best route is here. Do I store the snapshot into a dictionary then filter dictionary to "pending" or do this within the snapshot call itself?
friends & users JSON structure in my Firebase Database (as below;)
{
"friends" : {
"YPQYLtXnMbbmFugrJJPYe6rOIJg2" : {
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
},
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : {
"YPQYLtXnMbbmFugrJJPYe6rOIJg2" : "pending",
"ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : "pending",
"lNI9FxCErqMUNiW43yiDpkNoljg1" : "pending"
},
"ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : {
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
},
"lNI9FxCErqMUNiW43yiDpkNoljg1" : {
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : "pending"
}
},
"users" : {
"YPQYLtXnMbbmFugrJJPYe6rOIJg2" : {
"email" : "orsrzlqwmt_1488724206#tfbnw.net",
"name" : "Karen Alaedidibgghi Liangescu",
"profileURL" : "someURL"
},
"Z6PnyFKSR9MBMd9dfCEs0VMIOog2" : {
"email" : "rbwimniovp_1488724216#tfbnw.net",
"name" : "Patricia Alaefhcbebjid Warmansen",
"profileURL" : “someURL”
},
"ZyAV7PH4VHWnLyWrWaZty5C9RWT2" : {
"email" : "bhulxppahm_1488724211#tfbnw.net",
"name" : "Harry Alaeejdjagjga Greenestein",
"profileURL" : "someURL"
},
"lNI9FxCErqMUNiW43yiDpkNoljg1" : {
"email" : "axtinlmwes_1488724221#tfbnw.net",
"name" : "Maria Alaefehgadbdg Valtchanovsky",
"profileURL" : "someURL"
}
}
}
Any help greatly appreciated. Cheers!
Here's how you'd get started, once you have your parsed data you can use different methods to filter it which you can look up through different stackoverflow questions answered in the past.
let dbRef = FIRDatabase.database().reference.child("friends")
dbRef.observeSingleEvent(of: .value, with: { snapshot in
if let data = snapshot.value as? [String: [String: String]] {
for user in data {
// You'll now have a dictionary with neatly formatted values you can filter
}
}
})
Related
I am trying to add a set of data to a database by pushing the data to an array and adding them to the database. I have added my codes here
const com = {
company: "Com",
description: "Turn ...",
pointsNeeded: "500",
title: "Com",
PromoCode: Math.floor(Math.random()*1000000000),
imageLocation: "www...",
}
this.afDB.list('rewards/' + se.uid).push(com)
.then(next => {
console.log(next);
});
This is how my database look like
However, I would like the database to be like
"rewards" : {
"fH7GPHMFupW2JD8fxKnLOvHBZ0w1" : {
0: {
"PromoCode" :
"company" :
"description" :
"imageLocation" :
"pointsNeeded" :
"title" :
},
1" : {
"PromoCode" :
"company" :
"description" :
"imageLocation" :
"pointsNeeded" :
"title" :
}
}
}
Can anyone advice? I am not really sure what to do next.
Thanks
UPDATE:
const getUserReward = firebase.database().ref('rewards/').child(se.uid)
getUserReward.once('value', data => {
var rewardArray =[];
rewardArray.push(comchestReward)
getUserReward.update({rewards: rewardArray})
})
So this is the code I have so far but it is now
I am trying to rename the particular child field inside an array of an collection in MONGODB
{
"_id" : Id("248NSAJKH258"),
"isGoogled" : true,
"toCrawled" : true,
"result" : [
{
"resultsId" : 1,
"title" : "Text Data to be writen",
"googleRanking" : 1,
"isrelated" : false
},
{
"resultId" : 2,
"title" : "Text Data",
"googleRanking" : 2,
"isrelated" : true
}]
**I need to rename "isrelated" to "related" ** from the collection document
Using mongo Version 4.0
I Tried :
db.collection_name.update({}, { $rename: { 'result.isrelated': 'result.related'} } )
But it didn't worked in my case
As mentioned in official documentation $rename not working for arrays.
Please check link below:
https://docs.mongodb.com/manual/reference/operator/update/rename/
But you can do something like this
let newResult = [];
db.aa1.find({}).forEach(doc => {
for (let i in doc.result) {
newResult.push({
"resultsId" : doc.result[i]['resultsId'],
"title" : doc.result[i]['title'],
"googleRanking" : doc.result[i]['googleRanking'],
"related" : doc.result[i]['isrelated'],
})
}
db.aa1.updateOne({_id: doc._id}, {$set: {result: newResult}});
newResult = []
})
Although i have found a similar question on stackOverFlow MongoDB copy a field to another collection with a foreign key
I want to copy a field name from userdetails collection to user collection where userId in userDetails equals _id in user.
user collection
{
"_id" : ObjectId("5b97743bbff66e0be66283cc"),
"username" : "mmi_superadmin",
"accId" : "acc1"
}
{
"_id" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"username" : "client",
"accId" : "acc1"
}
userDetail collection
{
"_id" : ObjectId("5b97743bbff66e0be66283cd"),
"userId" : "5b97743bbff66e0be66283cc",
"name" : "mmi_superadmin"
}
{
"_id" : "5bab8a60ef86bf90f1795c44",
"userId" : "c21d580ea3ca5c7a1664bd5feb57f0c8",
"name" : "RAHUL KUMAR TIWARI"
}
Here is my query :
db.userDetails.find().forEach(
function(x) {
db.user.update( {_id :x.userId}, {$set: {name:x.name}});
}
);
This query is partially working. It only updates user documents where _id is of type string. User document with _id as ObjectId are not getting updated.
Please check your documents _id's (because in your example some _id's is not valid documents _id's. for example c21d580ea3ca5c7a1664bd5feb57f0c8 not a mongo _id) and use this query:
let usersIds = [];
db.user.find({"_id": {$type: 7}}).forEach(doc => {
usersIds.push(doc._id + '')
db.userDetail.find({
userId: {
$in: usersIds
}
}).forEach(doc => {
db.user.update(
{
"_id": ObjectId(doc.userId)
},
{
$set: {
"name": doc.name
}
},
{
multi: false,
upsert: false
}
)
})
})
if you have any question feel free to ask
I followed this tutorial to make an app with firebase sync and authentication. But now, I don't know how to make a query to search among all children because these children have a lexicographical-based key.
To be more specific, from this example, how do you query all "currentCity" from all "users"
{
"users" : {
"1e2f048f-a3a0-4190-afad-81d724ed1997" : {
"currentCity" : "Arrecifes",
"currentCountry" : "ARG",
"currentState" : "BSA",
"day" : {
"domingo" : true,
"jueves" : true,
"martes" : false,
"miercoles" : true
},
"gender" : "Masculino",
"name" : "Guillermo H Acosta",
"position" : "Medio",
"registered" : true,
"timeSince" : "22:00",
"timeUntil" : "23:00",
"whereToPlay" : "Güemes"
},
"39c6ccf9-61ec-446e-9af3-3a87810fab71" : {
"currentCity" : "Alvear",
"currentCountry" : "ARG",
"currentState" : "CRR",
"day" : {
"jueves" : true,
"miercoles" : true,
"viernes" : true
},
"gender" : "Masculino",
"name" : "Guillermo Acosta",
"registered" : true,
"timeSince" : "21:00",
"timeUntil" : "23:00"
},
"4991bdc9-dfc4-4ff5-ab81-0e28f1b3ab53" : {
"currentCity" : "Cordoba",
"currentCountry" : "ARG",
"currentState" : "COR",
"day" : {
"miercoles" : true,
"viernes" : true
},
"gender" : "Masculino",
"name" : "Hsjja",
"timeSince" : "02:00",
"timeUntil" : "03:00"
},
"509364fd-388b-42ff-91ed-0811811a4ff3" : {
"day" : {
"jueves" : true,
"lunes" : true,
"martes" : false,
"sabado" : true
},
"name" : "Guillermo Acosta",
"position" : "Delantero",
"timeSince" : "13:00",
"timeUntil" : "14:00",
"whereToPlay" : "Güemes"
}
}
}
With the Firebase Database, you either get a node or you don't get that node. There is no way to get a part of each nodes in a query.
So if you want load just the cities of all users, you should keep a node that contains just that information:
{
"userCurrentCities" : {
"1e2f048f-a3a0-4190-afad-81d724ed1997" : "Arrecifes",
"39c6ccf9-61ec-446e-9af3-3a87810fab71" : "Alvear",
"4991bdc9-dfc4-4ff5-ab81-0e28f1b3ab53" : "Cordoba",
}
}
This will parse through each node grabbing the currentCity key. If currentCity is NULLABLE, I would recommend housing it in a separate node.
let ref = // path to users here
ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
// Put all of the users into a dictionary
let postDict = snapshot.value as! [String : AnyObject]
for user in postDict {
// Set each child of the user as a dictionary
let data = activity.1 as! [String : AnyObject]
// Get the specific child
let currentCity = children["currentCity"] as! String
// Do something with the data
print(currentCity)
}
})
I'm getting this:
"users" : {
"simplelogin:129" : {
"-JbX-CJuMDf1H8-yCETV" : {
"gold" : {
"receive" : {
"amount" : 1,
"date" : 1416835946409,
"email" : "you#me.com"
}
}
},
But I need something like this:
"users" : {
"simplelogin:130" : {
"date" : 1416834728422,
"email" : "yam#dam.com",
"id" : "130",
"gold" : {
"receive" : {
"-JbWva7Gh9Y3RarlF77h" : {
"amount" : 1,
"date" : 1416834737201,
"email" : "you#do.com"
},
If I use .update instead of .push, I get the file structure I need, but then it overrides it for each transaction instead of adding a new one each time.
receive.orderByChild("email").equalTo( $scope.user.email ).once('child_added', function(snap) {
snap.ref().push({gold: {receive: myGold}}).then(function() {
});
Your question is not very clear, but I assume that you want to push the new node deeper in the tree.
simplelogin:130
date: ...
email: "yam#dam.com"
id: 130
gold
received
-JbX-CJuMDf1H8-yCETV
amount: 1
date: ...
email: "yam#dam.com"
To put the data into gold/received, you'd do:
snap.ref().child('gold/receive').push(myGold);
So you first find the node you want to add something to and only then call push to add a new child (with a new unique id) there.