I have found a collection in one of our MongoDB databases with the name my.collection.
Is there a way to access this collection from the MongoDB shell, despite it having a point in the name?
> db.my.collection.findOne();
null
I'm pretty sure that that is not correct.
try this instead:
db["my.collection"].findOne();
you run into the same issue with hyphens or any other name that does not match on
[a-zA-Z_$][0-9a-zA-Z_$]
This limitation comes from valid named for javascript object properties.
if collection name is "my.collection"
db.my.collection.findOne(); // OK
null
if collection name is "my.1.collection"
db.my.1.collection.findOne(); // Not OK
SyntaxError: missing ; before statement
Fix:
db["my.1.collection"].findOne(); // Now is OK
null
Another foolproof method is:
db.getCollection("_SCHEMA").find()
While in the case of a underscore in the name, still cause a error with #Laura answer:
> db["_SCHEMA"].find()
2016-07-18T17:44:16.948+0200 E QUERY [thread1] TypeError: db._SCHEMA is undefined :
#(shell):1:1
Your code is correct. If it returns null it means that the collection is empty.
Related
I try to update a field in the MongoDb like this
https://mongoplayground.net/p/_jFYJGi8z46
However i get this error
fail to run update: write exception: write errors: [The array filter for identifier 'e2' was not used in the update { $set: { sizes.$[e1].wares$[e2].reserved: "2" } }]
I can't find a bug :( ... it must be a very simple one
Right... one dot was missing :( This is solved
https://mongoplayground.net/p/obdFTr6G1kj
I use a function insertOne to insert an object. I have place correct colon and also correct commas then why this error come. Thanks in Advance.
This is my code
db.items.insertOne({Name:"Oppo",Price:"45000",Ritems:"345",Sold:"45","Rating:"3.5",once"twos"})
Error is this
uncaught exception: SyntaxError: missing : after property id :
#(shell):1:78
>
I think that your "" are misplaced. I think this way would be better ??
db.items.insertOne({Name:"Oppo",Price:"45000",Ritems:"345",Sold:"45",Rating:"3.5",once:"twos"})
Also the error message is telling you about the Id column, you should check if you need to set it yourself or if your database has an auto-increment ID.
I'm trying to use update_all to update any records that is missing a key in a JSON stored in a table cell. ids is the ids of those records and I've tried the below...
User.where(id: ids).
update_all(
"preferences = jsonb_set(preferences, '{some_key}', 'true'"
)
Where the error returns is...
Caused by PG::SyntaxError: ERROR: syntax error at or near "WHERE"
LINE 1: ...onb_set(preferences, '{some_key}', 'true' WHERE "user...
The key takes a string value so not sure why the query is failing.
UPDATE:
Based on what was mentioned, I added the parentheses and also added / modified the last two arguments...
User.where(id: ids).
update_all(
"preferences = jsonb_set(preferences, '{some_key}', 'true'::jsonb, true)"
)
still running into issues and this time it seems related to the key I'm passing
I know this key doesn't currently exist for the set of ids
I added true for create_missing so that 1 isn't an issue
I get this error now...
Caused by PG::UndefinedFunction: ERROR: function jsonb_set(hstore, unknown, jsonb, boolean) does not exis
some_key should be a key in preferences
You're passing in raw SQL so you are 100% responsible for ensuring that is actually valid SQL. What you have there isn't. Check your parentheses:
User.where(id: ids).
update_all(
"preferences = jsonb_set(preferences, '{some_key}', 'true')"
)
If you look more closely at the error message it was telling you there was a problem precisely at the introduction of the WHERE clause, and right after ...true' so that was a good place to look for problems.
Syntax errors like this can be really annoying, but don't forget your database will usually do its best to pin down the place where the problem occurs.
I created a collection named "stats" but whenever I try to delete it by using db.stats.drop() cmd line. It shows an error.
TypeError: db.stats.drop is not a function
Even tried this.
db["stats"].drop
Any solution?
if the collection exists db.stats.drop() should work (depending on the mongodb version maybe) or else perhaps db.getCollection("stats").drop();
but if all else fails, run: db.runCommand({drop : "stats"}) this should drop it if it exists without any error at least.
Can not delete the collection from the shell,
The thing that the collection is available and my php script is accessing it (selecting|updating)
But when I used:
db._registration.drop()
it gives me an error:
Date, JS Error: TypeErrorL db._registration has no properties (shell): 1
The problem is not with deleting the collection. The problem is with accessing the collection. So you would not be able to update, find or do anything with it from the shell. As it was pointed in mongodb JIRA, this is a bug when a collection has characters like _, - or .
Nevertheless this type of names for collections is acceptable, but it cause a problem in shell.
You can delete it in shell with this command:
db.getCollection("_registration").drop()
or this
db['my-collection'].drop()
but I would rather rename it (of course if it is possible and will not end up with a lot of changing).
You can also use:
db["_registration"].drop()
which syntax works in JS as well.
For some reason the double quotes "_registration" did not workfor me .. but single quote '_registration' worked