Unable to access MongoDB collection from shell when the name contains '-' - mongodb

I have a collection named GoldenGlobes-emotion in my MongoDB 2.6.9
I found I can not access this collection from the MongoDB shell
When ever I try to access the collection, for example
db.GoldenGlobes-emotion.findOne()
I always got this:
ReferenceError: emotion is not defined
But it works well when I access the collection form Python with PyMongo.
Is this a shell bug?
Or '-' is a reserved character?

Try db["GoldenGlobes-emotion"].findOne().
The MongoDB shell is a Javascript interpreter. Javascript does not allow hyphens in variable names, because it interprets them as the minus-operator. However, you can also access object-fields with string literals by using the array-syntax. In that case, this restriction does not apply.

Related

Renaming a Collection Using Pymongo

I realize that a collection can be renamed in MongoDB using
db["old_name"].renameCollection("new_name")
But is there an equivalent in PyMongo? I tried the following, but it didn't work either.
db["old_name"].rename_collection("new_name")
According to the documentation, the method is simply named rename.
rename(new_name, session=None, **kwargs)
new_name: new name for this collection
session (optional): a ClientSession
**kwargs (optional): additional arguments to the rename command may be passed as keyword arguments to this helper method (i.e. dropTarget=True)
May be you can use rename in pymongo, just use
db.oldname.rename('newname')
You can check the link:
https://api.mongodb.com/python/current/api/pymongo/collection.html?highlight=rename
An admin command to rename a collection can be executed in like manner.
query = {
'renameCollection': '<source_namespace>',
'to': '<target_namespace>',
}
client.admin.command(query)
query = bson.son.SON([
('renameCollection', 't1.ccd'),
('to', 't2.ccd2'),
])
print(query)
self.mgclient.admin.command(query)
Need to use bson.son.SON, as dict is unordered. Please refer to:
http://api.mongodb.com/python/current/api/bson/son.html#bson.son.SON
http://api.mongodb.com/python/current/api/pymongo/database.html
Note the order of keys in the command document is significant (the
“verb” must come first), so commands which require multiple keys (e.g.
findandmodify) should use an instance of SON or a string and kwargs
instead of a Python dict.

MongoDB full text search with haskell driver

Is there a possibility to use the full text search of mongoDB with the haskell driver?
I found the 'runCommand' in the haskell API, but it expects a Document as parameter. That's fine for all the other commands that mongodb can run, but the syntax for a text command is:
db.collection.runCommand( "text", {search : "something"})
So I don't know how I'll get the "text" as first parameter in front of the Document.
Thanks
The text-command can be written in another structure:
{ text: your_collection
, search: your_text
, filter: your_filter
, limit: your_limit
, project: your_projection
}
I had my suspicion, since all "runCommand"-action have the same structure. So I tried to apply that structure to the text-command - but without success. Then I remembered that aggregate has another structure too and tried that, but that didn't work either. Finally, I found the answer in a google group entry of the Java driver.

Can not delete collection from mongodb

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

How to use distinct in MongoVue?

I need to find out distinct values(for example : CreationDate or SourceSystem) in MongoDB using MongoVUE. FYI, I can only use the trial version of the same.
I don't think you can do that using MongoVUE. You can do it through MongoDB shell running a command like this:
db.[Collection Name].distinct({Property Name})
ex: db.students.distinct('age')
db.students.distinct('age').length; // gives you the record count
I usually find SQL to Mongo Mapping Chart page useful in these case ( http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart )
From having a quick look, I can't see how you can either, as the "Find" feature forces you to use:
db.[collectionName].find({...})
so doesn't allow you to do:
db.[collectionName].distinct({...})
I recommend using the normal command line executable for Mongo instead of MongoVUE, then you can use the commands from the 10Gen documentation:
http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

MongoDB Shell - access collection with period in name?

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.