Accessing db from stored javascript - mongodb

I have a stored JS fucntion. From the function I would like to search a collection with a db.bannedwords.find().toArray(); It works when i call the function from db.eval, however if I try to use the function in a $where i get this error error: { "$err" : "ReferenceError: db is not defined", "code" : 16722 } Is because I'm already accessing the DB? and is there a work around?

Related

How to call server side javascript function created in MongoDB

I have created one function using MongoDB shell but I am unable to use this function and also don't know how to get the function definition to edit or check.
db.system.js.insertOne({ _id : "getSessions" , value : function (clientid){ //some code }})
and I used db.eval(getSessions(1)) for call this function but facing error ReferenceError: getSessions is not defined
Please help me for call this function and also I want to get function definition from MongoDb Shell.

mongo: collection not found ReferenceError: auditoria is not defined

I'm getting this message on a very simple find command:
rs0:PRIMARY> use MPI
switched to db MPI
rs0:PRIMARY> show collections;
hes-auditoria-mpi-fhir
hes-auditoria-schemas
rs0:PRIMARY> db.hes-auditoria-mpi-fhir.find();
2021-05-20T14:20:05.502+0200 E QUERY [js] ReferenceError: auditoria is not defined :
#(shell):1:1
any ideas?
Use getCollection helper:
db.getCollection("hes-auditoria-mpi-fhir").find()
It's been added exactly for this purpose. From the docs:
db.getCollection(name)
Returns a collection or a view object that is functionally equivalent to using the db.<collectionName> syntax. The method is useful for a collection or a view whose name might interact with the mongo shell itself, such as names that begin with _ or that match a database shell method.

Mongodb: How to drop a collection named "stats"?

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.

How can I test, if scripting is disabled in my MongoDB

I want to make my mongoDB more secure. I read, that I can start it with --noscripting to deny JavaScript. I tried to verify, that this is working.
When I started the DB like this
mongod --dbpath /var/lib/mongodb --noscripting --fork --logpath /var/log/mongodb/mongod.log
and then I tried to use the $where-Operator like it is described here:
http://docs.mongodb.org/manual/reference/operator/query/where/
for example:
db.myCollection.find({$where: "_id == 1"})
it is returning:
Error: error: {
"$err" : "Can't canonicalize query: BadValue no globalScriptEngine in $where parsing",
"code" : 17287
}
Is this the answer, I should expect? Or is it pointing to another failure?
This is expected behavior since the --noscripting option disables the scripting engine and all the $where operator does is JavaScript evaluation as mention in the official documentation;
Use the $where operator to pass either a string containing a JavaScript expression or a full JavaScript function to the query system. The $where provides greater flexibility, but requires that the database processes the JavaScript expression or function for each document in the collection. Reference the document in the JavaScript expression or function using either this or obj .
Also the error message is pretty clear.
..."$err" : "Can't canonicalize query: BadValue no globalScriptEngine in $where parsing"
Note the no globalScriptEngine part of the message.

Filter on collection of NSManaged Obejct does not work

I try to apply filter "by date" on my Comment's object collection, but the classic syntax that i use usually dosen't work! I get some error message!
Here you can find error message :
here an exemple of the syntaxe of my filter :
And here you find my Comment object :
Do you have any idea : how can I do that ?