Working with '$' as a character

just an observation, I needed to add a “$” character to a string in a project clause to create some self generating code .
{$concat['$','string']} does not work and I did not find a way to escape the $ character,
but did find that {$concat['\\$','string']} produced ‘\$string’ and { $replaceOne: { input: {$concat: ['\\$',"$"]}, find: '\\', replacement: '' } } produced the required “$string”.
Or am I missing a simpler way of doing this?

Welcome to the MongoDB community @Ido_Lelie!

You can use the $literal expression to avoid evaluating values like those containing a $:

> db.myCollection.aggregate([
	{ $project: {
		myString: {
			$concat: [ { $literal: "$"}, "string" ]
[ { _id: ObjectId("63ec193b85a71b2dc61637c7"), myString: '$string' } ]



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