I have a collection I'm unable to drop, I'm assuming that the "-" in its name is a special character. In MongoDB, what is the best way to escape special characters?
> db.tweets.drop();
true
BUT
> db.tweets-old.drop();
ReferenceError: old is not defined (shell):1
I've tried to escape with quotes (both single and double) and a slash, but nothing works.
The following works:
db["tweets-old"].drop();
It's called the square bracket notation, which allows you to use special characters in property names.
So does:
db.getCollection("tweets-old").drop()
And that has been around for a while now.
In addition, the method call also mimics what is the general "get a collection" accessor method in all officially supported MongoDB drivers. So the suggestion here is that you get used to using it this way, since the "named collection" accessor is how it is generally done.
Related
Is there any way to escape SQL Like string when using "sqlLike" and "sqlLikeCaseInsensitive"?
Example: I want a match for "abc_123". Using "_______" (7 underscores) would also return "abcX123", how can I enforce "_" as the 4th character?
If you issue the query in persistence, this is actually not a mdriven issue but an SQL issue as mdriven converts the Expression into SQL. So if you really want to restrict the results to underscores only take a look to this question:
Why does using an Underscore character in a LIKE filter give me all the results?
The way to escape the underscore may depend on the needs of your SQL database as the different answers indicate.
I'm coming across a strange situation where I cannot search on string tags that end with a special character. So far I've tried ) and ].
For example, given a Fruit index with a record with a tag apple (red), if you query (using the JS library) with tagFilters: "apple (red)", no results will be returned even if there are records with this tag.
However, if you change the tag to apple (red (not ending with a special character), results will be returned.
Is this a known issue? Is there a way to get around this?
EDIT
I saw this FAQ on special characters. However, it seems as though even if I set () as separator characters to index that only effects the direct attriubtes that are searchable, not the tag. is this correct? can I change the separator characters to index on tags?
You should try using the array syntax for your tags:
tagFilters: ["apple (red)"]
The reason it is currently failing is because of the syntax of tagFilters. When you pass a string, it tries to parse it using a special syntax, documented here, where commas mean "AND" and parentheses delimit an "OR" group.
By the way, tagFilters is now deprecated for a much clearer syntax available with the filters parameter. For your specific example, you'd use it this way:
filters: '_tags:"apple (red)"'
I have a collection I'm unable to drop, I'm assuming that the "-" in its name is a special character. In MongoDB, what is the best way to escape special characters?
> db.tweets.drop();
true
BUT
> db.tweets-old.drop();
ReferenceError: old is not defined (shell):1
I've tried to escape with quotes (both single and double) and a slash, but nothing works.
The following works:
db["tweets-old"].drop();
It's called the square bracket notation, which allows you to use special characters in property names.
So does:
db.getCollection("tweets-old").drop()
And that has been around for a while now.
In addition, the method call also mimics what is the general "get a collection" accessor method in all officially supported MongoDB drivers. So the suggestion here is that you get used to using it this way, since the "named collection" accessor is how it is generally done.
I'm reading over the MongoDB manual. Some examples, have quotes around the key values, e.g: db.test.find({"_id" : 5}) and others don't, e.g: db.test.find({_id : 5})
Both quoted and un-quoted versions work. But I'm wondering if there are some nuanced difference here I don't know about or is one a preferred best practice?
Thanks.
In JavaScript (the language of the MongoDB shell) those are treated exactly the same. The quotes are needed, however, when a key contains a period like when you're using dot notation to match against an embedded field as in:
db.test.find({"name.last": "Jones"})
My preference is to not use the quotes unless they're needed.
I have a collection I'm unable to drop, I'm assuming that the "-" in its name is a special character. In MongoDB, what is the best way to escape special characters?
> db.tweets.drop();
true
BUT
> db.tweets-old.drop();
ReferenceError: old is not defined (shell):1
I've tried to escape with quotes (both single and double) and a slash, but nothing works.
The following works:
db["tweets-old"].drop();
It's called the square bracket notation, which allows you to use special characters in property names.
So does:
db.getCollection("tweets-old").drop()
And that has been around for a while now.
In addition, the method call also mimics what is the general "get a collection" accessor method in all officially supported MongoDB drivers. So the suggestion here is that you get used to using it this way, since the "named collection" accessor is how it is generally done.