Сreating a database object by the value of a variable

Hello!

There is an object in the database, in which there can potentially be up to one and a half hundred objects. I would like every object to be named like this:

Screenshot_1

For this example, the names of the objects are hardcoded in the code:

"roles.top.wins": wins,
"roles.top.matches": matches

How do I make the named object be created by the value that is in the variable? Code like
object.${Id}: {} and object[id]: {} doesn’t work.

Thanks!

Hello @Ender_Wiggin, welcome to the MongoDB Community forum!

This is JavaScript related coding. You can try this, for example:

let topVar = 'top' 
let supportVar = 'support'
roles[topVar].matches = 5
roles[supportVar].wins = 3

If you are using many variables like that, it is probably useful to design your document like this, for example:

roles: [
    { role: "top", mathes: 12, wins: 5 },
    { role: "support", mathes: 3, wins: 3 },
    // remaining 100 odd sub-documents...
]

This design benefits from using the array query and update operators as well as indexing on the array field.

1 Like

Hello!

Code like this doesn’t work
roles[topVar].matches = 5

I am working with node.js and mongoose.
The idea is that the player plays champions (of which there are 155 in the game) and statistics for each champion should go into the database. Initially, the object with champions is empty, if the champion is not in this database object, then it is added there, and if there is, then the statistics increase (frags, assists, deaths, etc.)

The same idea is implemented with roles, but there are only 5 roles in the game, so I can afford to hard-code "role.support.wins": wins

In a role situation, I am making a comparison:

if (obj[key].role === 'TOP') {
	champion.updateOne({id: key}, {
		$inc: {
			"roles.top.wins": wins,
			"roles.top.matches": matches
		}
	}, {upsert: true})
} 

But how can I do something like that when there may be 155+ coincidences, I don’t know.

That’s what you get when you want the flexibility of non putting quotes around string literal. You do not know when it is a variable or a literal.

But they introduced https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names

so try

let topVar = 'top" ;
let supportVar = 'support' ;
let matches_field = topVar + ".matches" ;
let wins_field = supportVar + ".wins" ;
roles = { [matches_field] : 5 ,
    [wins_field] : 3 } ;
1 Like

Wohoo!

This syntax works

const url = `champions.${champName}`;

.....

 [url]: {
	name: champName,
	matches: matches,
	wins: wins,
	assists: assists
},

But another problem, I need to summarize the statistics after each match, so i got error:

[url]: {
	name: champName,
	$inc: {
		matches: matches,
		wins: wins,
		assists: assists
	}
},

MongoError: The dollar ($) prefixed field '$inc' in 'champions.Darius.$inc' is not valid for storage.

How can i fix it? Thanks!

I did it!

summoner.updateOne({sumId: sumId}, {
	sumId: sumId,
	sumName: sumName,
    [`champions.${champName}.name`]: champName
	$inc: {
		[`champions.${champName}.matches`]: matches,
		[`champions.${champName}.wins`]: wins,
		[`champions.${champName}.assists`]: assists
	},
	
}, {upsert: true})

Thanks a lot, guys!!! Thanks from the Russia with love!!!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.