Update a string in a collection

Hey!

Im kinda new to Meteor and struggling, i would greatly appreciate some help with this. It seems easy, but i cant get it right.

useraccounts.findOne({username: "Glutch"})
Object {_id: "zkmRN3mh538wv2Y2Z", username: "Glutch", money: 106, xp: 40, level: 2}

This is what i get in console.

I simply want to search for the user “Glutch” and increase his money, xp, or level.

im trying with this

useraccounts.update(Meteor.user().username, {$set: {money: Session.get('money')} });
useraccounts.update(Meteor.user().username, {$set: {xp: Session.get('xp')} });
useraccounts.update(Meteor.user().username, {$set: {level: Session.get('level')} });

(i know it might be stupid with 3 of them, but im not sure how to combine them)

However, the above doesnt work.

Also. If a new user logs in, and uses the .update function. Does it create one if one was not previously there?

Also. How do i create a “default” collection for new users?

Thanks in advance!
Regards,
Glutch

var modifier = {money: <...>, xp: <...>, level: <...>};
useraccounts.upsert({_id: Meteor.userId()}, {$set: modifier});

My code now looks like this

var modifier = {money: Session.get('money'), xp: Session.get('xp'), level: Session.get('level')};
useraccounts.upsert({_id: Meteor.userId()}, {$set: modifier});

But nothing seems to happen. I see nothing in console. No errors, nothing. If i use
useraccounts.findOne(); i see only one account with “non-updated” values

EDIT:
It works, i had to clear my database ^^
((Meteor reset) in terminal cleans the mongodb) for those wondering / googling in the future.

And one more note - none of these are strings :smiley:

Haha yea :smile:

Is there anyway to grab “money” directly from this?

useraccounts.findOne({username: Meteor.user().username}) 

Like this

useraccounts.findOne({username: Meteor.user().username}).money()

or something ^^

My object looks like this

Object {_id: "XcThqvH9K8YnmdeBB", money: 116, xp: 40, level: 1, username: "Glutch"}

Well, it return object which contain money property, so you can do

var whatever = useraccounts.findOne({username: Meteor.user().username}).money

I would still suggest limiting returning fields too, so it will return only _id and money, so

var whatever = useraccounts.findOne({username: Meteor.user().username},{money: 1}).money

Awesome, thanks

However i found this solution to be handier

var currUser = useraccounts.findOne({username: Meteor.user().username});

currUser.level
currUser.money

etc :smile:

Yeah, it happend because of selector {_id: Meteor.userId()}. I found your previous version Meteor.user().username not clear according to DB architecture. Imagine that one day your user will change username? OMG)))

It is not an issue on the client-side. When you limiting fields on the client cursor just call _.omit() for every document and it could be expensive.
Same thing when Collection.findOne(<stringId>) is faster than Collection.findOne({_id: <stringId>}).

Oh, thats true! Thanks alot. That one could be real pain hahaha

var test = useraccounts.findOne({username: Meteor.user().username});
  if (test === "undefined") {
    useraccounts.upsert({_id: Meteor.userId()}, {$set: {level: 1}});
    useraccounts.upsert({_id: Meteor.userId()}, {$set: {xp: 0}});
    useraccounts.upsert({_id: Meteor.userId()}, {$set: {money: 100}});
  };

Im trying to do something like this.

If the user is new, he should be level 1, with 0xp, and 100 money.

The code above doesnt work, and i cant figure ut why.

Ive tried with if(test === null) if(test === undefined) if(test === “undefined”)
Nothing seems to work

Also. where should i put it? If i put it at the top of main.js. I get errors

test === undefined. Man you should $set 2-3 levels of JS for yourself)))

if useraccounts.find(<selector>).count() == 0 {
	var modifier = {level: 1, xp: 0, money: 100};
	useraccounts.upsert(<selector>, {$set: modifier});
}

The safest way for checking for undefined is if (typeof test === "undefined"), primarily because the undefined value may have been overwritten and not be undefined any more! However, @mrzafod’s check is cleaner in the context of a Meteor collection.

Have you actually checked in your browser console that test is undefined with that search?

Thanks guys, i got it working :smile:

userInfo: function () {
      if (useraccounts.find({_id: Meteor.userId()}).count() === 0) {
        var modifier = {username: Meteor.user().username, level: 1, xp: 0, money: 100};
        useraccounts.upsert({_id: Meteor.userId()}, {$set: modifier});
      }
      return oliverUsers.findOne({_id: Meteor.userId()});
    }

I forgot to add the username if it was not found. haha