I am trying to add a simple transaction to a firebase database, however, I'm not having luck getting around the firebase generated keys for each account. here is my JSON:
{
"accounts" : {
"-KRPSyO4B48IpBnHBTYg" : {
"newRecord" : "100",
"email" : "",
"provider" : "",
"userId" : ""
}
},
"products" : {
"-KUKRafaNurpFhGF4jQa" : {
"name" : ""
}
},
}
I want to add a count to "newRecord" to the same level as userId, provider and email. However I am not successful with the following, or any variation so far:
firebase.database().ref('accounts/' + accounts.id).transaction(function(value) {
if (value) {
value.newRecord++;
}
return value;
});
Thanks in advance!
You should refer the value directly while using the transaction.
firebase.database().ref('accounts/' + accounts.id+'/newRecord').transaction(function(value) {
if (value) {
value++;
}
return value;
});
Related
I'm currently working with the node library firebase and I want to search for a single value by using database refs.
I've been able to search for every single record on the realtime-database by returning the ref itself from my CharacterService's getAll method, but I can't seem to find a way to do it for jus a single record.
Firebase (Configuration file)
import firebase from 'firebase';
import config from '../config';
if (firebase.apps.length === 0) firebase.initializeApp(config.firebase);
const database = firebase.database();
export default database;
CharacterService
import firebase from 'config/firebase';
const db = firebase.ref('/character');
class CharacterService {
getAll () {
return db;
}
create (character) {
return db.push(character);
}
update (key, value) {
return db.child(key).update(value);
}
delete (key) {
return db.child(key).remove();
}
deleteAll () {
return db.remove();
}
}
export default new CharacterService();
Database records
{
"character" : {
"-MLEm-8ehLQ4repmeKtl" : {
"age" : 36,
"exp" : {
"current" : 0,
"toLvlUp" : 125,
"total" : 0
},
"gender" : "Male",
"level" : 1,
"name" : "Aleksandr Brightwood"
},
"-MLEm-7fhLQ5rehltKjk" : {
"age" : 22,
"exp" : {
"current" : 0,
"toLvlUp" : 125,
"total" : 0
},
"gender" : "Female",
"level" : 1,
"name" : "Sarah Autumn"
}
}
}
You can certainly get a child node using its key.
const childRef = firebase.ref('/character').child(key);
Now you have a Reference you can use to get only the data under that nested child.
I suggest reviewing the documentation for more information.
I have two collection that contains:
"DeviceId" : "",
"FirstName" : "",
"LastName" : "",
"AllowDomains" : "",
"JobLocationName" : ""
and so on. I write this query that first check that deviceId in collection1 is the same of deviceId in collection2. If were same then do nothing otherwise must copy into temp Collection.
but first:
It does not compare at all and these code copy all documents?
and second, middle of copy process I got error?
function compareCollection (dv, F2){
var compare = F2.find().forEach(function(doc1){
if (doc1.DeviceId === dv ) {
return false;
} else {
insertToColl(doc1.DeviceId)
}
});
}
var result;
db.getCollection('FinalLocation').find({}, {DeviceId: 1}).forEach(function(u)
{
result = u.DeviceId;
});
compareCollection(result , db.getCollection('FinalLocation_'));
function insertToColl(info){
var post = {
"DeviceId" : info,
"FirstName" : "",
"LastName" : "",
"AllowDomains" : "",
"JobLocationName" : ""
};
db.getCollection('tempColl').save([post], function (err, result) {
if (err) {
console.log("ERROR ", err);
}
else {
console.log("SUCCESS INSERTED in to users collection _is are ", result.length, result)
}
});
}
these error appear when mongo want's insert data into collection:
2017-09-26T15:41:47.467+0330 E QUERY [thread1] Error: can't save a number or string :
DBCollection.prototype.save#src/mongo/shell/collection.js:615:1
insertToColl#(shell):2:3
compareCollection/compare<#(shell):7:7
DBQuery.prototype.forEach#src/mongo/shell/query.js:501:1
compareCollection#(shell):2:17
#(shell):1:1
I created a function that adds a UID to the database along with an item's state:
changestate(item) {
var postData = {
state: "listed",
};
var user = firebase.auth().currentUser;
var uid = user.uid;
var updates = {};
updates['foods' + '/' + item.$key + '/' + 'state' + '/' + uid] = postData;
return firebase.database().ref().update(updates);
}
I want to create a query that only shows the data corresponding to that UID. In a previous query I was using:
getLists(): FirebaseListObservable<any> {
return this.db.list('/foods', {
query: {
orderByChild: 'state',
equalTo: 'listed'
}
});}
This is the structure of my database:
{
"foods" : {
"foodID1" : {
"category" : "Produce",
"foodname" : "Apples",
"state" : {
"aePQkvozV6gehP7ihjN0OWCltKu2" : {
"state" : "listed"
}
}
},
"foodID2" : {
"category" : "Dairy",
"foodname" : "Cheese",
"state" : {
"aePQkvozV6gehP7ihjN0OWCltKu2" : {
"state" : "listed"
}
}
}
}
}
What do I need to do to show the items that correspond to a signed in user's UID?
The orderByChild property can take a path. So the code then simple becomes:
getLists(): FirebaseListObservable<any> {
return this.db.list('/foods', {
query: {
orderByChild: 'state/'+firebase.auth().currentUser.uid+'/state',
equalTo: 'listed'
}
});
}
This requires that the user is signed in of course. To test the query without a signed-in user, you can use a hard-coded value:
getLists(): FirebaseListObservable<any> {
return this.db.list('/foods', {
query: {
orderByChild: 'state/aePQkvozV6gehP7ihjN0OWCltKu2/state',
equalTo: 'listed'
}
});
}
Im trying to write a function that will search for an object by ID and whether or not a value is contained in an embedded array within the object.
{
"_id" : ObjectId("569bea91c0e1fee4063527ac"),
"user" : ObjectId("568c65174fee132c36e199dd"),
"votes" : 9,
"image" : "./modules/deals/client/img/uploads/1546f914dba7e1732ea853cd70d79148.jpg",
"price" : "12.95",
"retailer" : "argos.co.uk",
"voters" : [{
"user" : ObjectId("568c65174fee132c36e199dd"),
},
{
"user" : ObjectId("568c65174fee132c36e199dd"),
},
{
"user" : ObjectId("568c65174fee132c36e199dd"),
}]
I would like to search by the _id and the voters.user.
I believe i need to finish this function correctly
exports.dealByIdAndVoter = function(req, res) {
Deal.count({
$where: function () {
}
},
function(err, dealByIdAndVoter) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
console.log(dealByIdAndVoter);
var data = {};
data.count = dealByIdAndVoter;
res.json(data);
}
});
};
If you do not need to use the $where function, you can construct the query with the $or operator in this manner:
Deal
.find({
$or: [
// Match by _id
{ _id: req.id },
// Match by any user in the 'voters' array with a matching 'user' field.
{ 'voters.$.user': req.id }
]
})
.count(function(err, count) {
// Handle error here
res.json(count);
});
I have a collection of data like this(for example) :
{
name : "john" ,
_id : "0"
},
{
name : "Richard" ,
parent_id : "0" ,
_id : "1"
},
{
name : "Kevin" ,
parent_id : "0" ,
_id : "2"
},
{
name : "William" ,
parent_id : "1" ,
_id : "3"
},
{
name : "George" ,
parent_id : "3" ,
_id : "4"
}
I'm trying to write a function to receive an _id and return all children in any depth of this node, for example for _id = 0 I need something like this :
[
{
name : "Richard" ,
parent_id : "0" ,
depth : "1" ,
_id : "1"
},
{
name : "Kevin" ,
parent_id : "0" ,
depth : "1" ,
_id : "2"
},
{
name : "William" ,
parent_id : "1" ,
depth : "2" ,
_id : "3"
},
{
name : "George" ,
parent_id : "3" ,
depth : "3" ,
_id : "4"
}
]
I write several recursive functions to iterate on my mongodb docs but the main problem is I can't handle callbacks (asynchronous) and don't know when and how can I end the recursive function.
How can I do this with mongodb and node.js?
Any idea can be useful ,thanks.
There is a 2 famous algorithm that you can use in order to achive your goal
BFS(Breath First search) and DFS(Depth First Search).
For this problem BFS is better than DFS because you can trace your tree in O(logn)
You can use DFS too but you have to implement it in recursive way and running time will be O(n) and because your are coding in node js you have to implement it in a asynchronous and it could be a little hard to implement it.
In order to implement BFS algorithm you have to use asynchronous while loop because you have to have mongo query in your while loop and if you use normal javascript your BFS won't work because we are talking about node js not php!!!
So first this is asynchronous while loop that I use in my BFS code
function asyncLoop(iterations, func, callback ,foo) {
var done = false;
var loop = {
next: function() {
if (done) {
return;
}
if (iterations) {
func(loop);
} else {
done = true;
if(callback) callback(foo);
}
},
isEnd : function(){
return done ;
} ,
refresh : function(it){
iterations = it ;
},
break: function() {
done = true;
callback();
}
};
loop.next();
return loop;
}
And this is BFS algorithm node js code:
function bfs (_id ,callback){
_id = String(_id);
var q = [] ,res = [] ;
db.tasks.findOne({ _id : _id }).lean().exec(function(err,root){
root.depth = 0 ;
q.push(root);
asyncLoop(q.length ,function(loop){
res.push(q[0]);
db.tasks.find({ _parent : q[0]._id }).lean().exec(function(err,new_nodes){
if(err) console.log(err);
else {
var d = q[0].depth ;
q.shift();
loop.refresh(new_nodes.length + q.length);
if(new_nodes.length > 0){
new_nodes.forEach(function(new_node){
new_node.depth = d+1 ;
q.push(new_node);
});
}
loop.next();
}
});
},function(){ callback(res) });
});
}
Note:I also save depth of each query so you can have depth of each query and know where this query is in tree.