How to call server side javascript function created in MongoDB - 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.

Related

In Grafana, how do I use a variable I made and give it as a value into a query search for a graph?

I am trying to take the value of a drop down I created in grafana, utilizing a query variable.
I am using SimpleJSON for my database.
I have tried this pattern where:
var is my variable that makes up the drop down menu
func is my function call for the database query that creates the graph
So then I wrote:
f.g.func[$var]
I get a Internal Server Error yet when I call func in kdb+ with the values directly, it works fine. So I think I'm passing in var incorrectly.
For grafana/kdb+ adaptor, the syntax is f.g.func["$var"]-- my above attempt lacks the quotes necessary

Meteor/MongoDB limiting the result

I am trying to find all documents and publish at most 5 from the results.
Following this section of the MongoDB doc, I am trying to do this:
Meteor.publish('teams', function () {
return Teams.find().limit(5);
});
Yet, in the server console, I get an exception:
Exception from sub teams id Pm6jKL8Sv3FSDSTfM TypeError: Object [object Object] has no method 'limit'
The following works fine:
Meteor.publish('teams', function () {
return Teams.find({}, {limit:5});
});
Why does the second way work, rather than the first? And where can I find documentation for it?
Meteor's collection API is somewhat different from that of the mongo API. find takes up to two parameters: a selector object, and an options object. options allows you to specify such things as sort, skip, limit and fields, in addition to the meteor-specific reactive and transform.

Accessing db from stored javascript

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?

Wrong distance in geonear method with Doctrine MongoDB ODM

I'm using DoctrineMongoDBBundle with Symfony2 and I've a problem with geocoordinates. This works fine but when the longitude is for example like that: 0.635467 the code doesn't work. I have more geocoordinates and only fails when it begins with 0. and the distance field is NULL.
This is my code:
$locations = $dm->createQueryBuilder('MyBundle:Location')
->field('id')->in($arrayIds)
->field('geocoordinates')
->geoNear($geocodes['lat'],$geocodes['lon'])
->getQuery()->execute()->toArray();
I'm following this link: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/geospatial-queries.html but with the geonear method.
The geoNear() query builder method is not intended to be used on a field. near() is the builder method that would follow a field() focus. You can see what both of these builder methods do in Builder.php within the doctrine/mongodb project. Note that geoNear() changes the query type (similar to what update() does). The query type is then checked in Query.php (follow the switch statement) and determines how we issue the query on the collection. Some are actual query operations, but things like map/reduce and geoNear are commands.
See if the following code works:
$dm->createQueryBuilder('MyBundle:Location')
->geoNear($geocodes['lat'],$geocodes['lon'])
->field('id')->in($arrayIds)
->getQuery()->execute()->toArray();
If not, please debug the values that Query.php passes to the Collection::near() method. Alternatively, you can debug the entire query array generated by the builder by using the Query::getQuery() method.

In MongoDB, how can I retrieve an item and then add to an embedded document within it?

I'm trying to do something similar to the following
http://mongoosejs.com/docs/embedded-documents.html
However instead of new BlogPost I'm trying to retrieve then add as follows
function addComment(id, comment, callback) {
Post.findOne(id, function(err, post) {
post.comments.push(comment);
});
}
I'm getting
TypeError: Cannot call method 'call' of undefined
What am I doing wrong? I left out the saving code for simplification it is crashing even without trying to save.
Assuming that the id parameter is an ObjectId or string (and not a query object), you should be calling findById instead of findOne.